Change today's date and time in php

后端 未结 5 948
孤城傲影
孤城傲影 2020-12-17 09:11

Is it possible to change php\'s current date and time (something like set_time(strtotime(\'1999-09-09\')) so when i call time() it will return me timestamp for 1999-09-09? I

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 09:48

    PHP doesn't have a date and time on its own. time() returns the current timestamp of the server's time. Depending on your operating system, it may well be that you (i.e. the user that runs your script's process) are actually not allowed to change the server date and time.

    Which asks a different question: Why do you want to do this?

    Better would be to implement your own time() function:

    class myTime
    {
    
        private static $offset = 0;
    
        public static function getTime()
        {
            return time() + self::$offset;
        }
    
        public static function setTime($time)
        {
            self::$offset = $time - time();
        }
    }
    

    And then, everywhere where you use time(), use myTime::getTime();. To set the time, use myTime::setTime($time) where $time is the timestamp you want to use.

提交回复
热议问题