tags:

views:

39

answers:

3

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.

+4  A: 

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"
steven_desu
You risk screwing up the system clock anyway. Example: you run the script at 11:59:55, set clock two hours back, finish, set it two hours forward, it now says 12:00:10. Unfortunately, you had a process scheduled to launch at 12:00:00 - will it launch? The answer is "nobody knows" - according to the clock, 12:00:00 never happened. (you need pretty high permissions to mess with the clock anyway, and if you're running the server as root, you're already in twenty kinds of trouble)
Piskvor
as i understand i need proper rights to change date thru shell. Not sure that it is possible for www-data or apache2 user. Anyway good point. Will check it out.
vooD
+1  A: 

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.

Spudley
+1  A: 

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 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.

Pelle ten Cate