Change today's date and time in php

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 need to set it globally for whole app.

EDIT

I already have one old app and i need to make possible to set up current date.
I can’t accept any custom functions. Imagine i should review all the code and replace every time() call. Even if i’ll do so then it will not help because there are functions like getdate() which i need to replace as well.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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"

Solution 3

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.

Solution 4

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.

Solution 5

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply