Change today's date and time in php

后端 未结 5 843
孤城傲影
孤城傲影 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.

    0 讨论(0)
  • 2020-12-17 09:53

    You could also check the Timecop PHP module, worked perfectly in my case.

    After installing it, you can travel back & forth in time with just one line of code.

    0 讨论(0)
  • 2020-12-17 09:56

    You may also try to use global $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1.0) and replace it on run-time.

    0 讨论(0)
  • 2020-12-17 10:04

    The time() functions actually asks the system clock for the time, so you'd have to update the time of the server's system clock. A few things to keep in mind:

    • This is done differently on a Unix server than a Windows server. If you want code compatible with both, you have to be careful
    • Sometimes (especially in shared hosting situations) you aren't allowed to change the system time
    • Changes to system time will remain after the script has executed unless you change it back later. Therefore you risk really screwing up the system clock.

    If you want to change the time globally for an entire app, I recommend having a static variable $timeOffset and create your own time() function as follows:

    function myTime(){
        global $timeOffset;
        return time()+$timeOffset;
    }
    

    If you've taken all of that into account and still want to change the system time, however, you would need to send a command to the shell using shell_exec()

    In Windows, the code would look like this:

    shell_exec("date 09-09-99"); // Use "date mm-dd-yy" or "time hh:mm:ss", respectively
    

    In UNIX, according to the date man page, the code would look like:

    shell_exec("date 0909hhmm1999"); // It says "date MMDDhhmiYYYY". I'm not sure how to set seconds, although I assume "mi" = "minutes"
    
    0 讨论(0)
  • 2020-12-17 10:05

    No it isn't. PHP gets its time from the system time. The only way you can change what PHP sees as the time is to change the system time.

    It may be feasible for you to do that using exec() or something similar, but probably not advisable.

    A better solution may be to create your own time function, which you can specify, using an offset.

    eg:

    function mytime() {
        return time()+set_mytime();
    }
    
    function set_mytime($new_time=null) {
        static $offset;
        if(!$new_time) {return $offset;}
        //$new_time should be a unix timestamp as generated by time(), mktime() or strtotime(), etc
        $real_time=time();
        $offset=$real_time-$new_time;
    }
    
    //usage:
    set_mytime(strtotime("1999-09-09");
    $faketime=mytime();
    .... do something that takes a bit of time ....
    $faketime2=mytime();
    

    That's just something I've thrown together quickly for you. I haven't tested it, so there may be (probably are) bugs, but I hope it gives you enough to get a solution.

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