Set time offset in php

前端 未结 1 991
春和景丽
春和景丽 2021-01-27 18:22

Is there some way to return all date returned by the date() function to be offset by some specific amount of time ? I tried date-default-timezone-set(), didn\'t seem to work on

相关标签:
1条回答
  • 2021-01-27 18:45

    You could also write your own version of the date() function which returns the date processed by the callback you require (adding an offset).

    <?php
    
    function date_offset($format, $offset)
    {
      $time = time() + $offset; # $offset should be in milliseconds
      return date($format, $time); # with your require display format $format
    }
    

    If you really want to just call date(), i.e. for sake of consistency, you could write your app in your own namespace and simply name your custom function date(). In that case, PHP's original date() function has to be called as \date().

    0 讨论(0)
提交回复
热议问题