vooD
vooD

Reputation: 2921

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.

Upvotes: 32

Views: 41076

Answers (6)

Roger Dueck
Roger Dueck

Reputation: 625

If you're running your PHP script from a command-line on Linux, the faketime utility can be used to change the system time seen by PHP (or any program). It also lets you change the rate at which time advances.

Note that times obtained from external services, such as via database queries (i.e. SELECT NOW()) will continue to show system time, unless they have also been launched with faketime.

Thanks to @zmf for the link to hnw/php-timecop (no longer maintained), which led me to the active fork kiddivouchers/php-timecop, where faketime was mentioned in the latest commit.

Upvotes: 0

zmf
zmf

Reputation: 562

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.

Upvotes: 20

Pelle
Pelle

Reputation: 6588

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.

Upvotes: 5

kenorb
kenorb

Reputation: 166919

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

Upvotes: -1

Spudley
Spudley

Reputation: 168853

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.

Upvotes: 1

stevendesu
stevendesu

Reputation: 16841

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"

Upvotes: 13

Related Questions