0

I have some PHP4 code in which I need to restore the system time after some code execution. I am looking for a simple statement with which I can store the current date/time, and restore it afterwards:

$oTime = function1();
...
do something else
...
function2($oTime);

In PHP5 you can pretty straightforward use date_timestamp_get() and date_timestamp_set(), but how to achieve this most simple if you have to use PHP4?

P.S. No questions about why I use PHP4. I don't like it either...

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 1
    Why do you need to alter the system time? It might be easier (and safer) to answer that? (after all, if one request takes the system time back, by an hour, then another comes in and takes it back another hour, then the first completes and restores the time, the second won't have the time it was expecting, and would restore to the first's working time) – Rowland Shaw May 16 '13 at 09:14
  • It is a test for a time-setting functionality. I change the time to a constant value, verify it, and set the time back (with the small subtelty of leaving out the time for the test itself). – Alex May 16 '13 at 09:15
  • Do you have NTP (Network Time Protocol) installed. You can use this to sync the servers time back to the correct time http://en.wikipedia.org/wiki/Network_Time_Protocol (its usually done at startup, but you can action it yourself - sync, change time, run script, sync) - linux command line will be `nptdate xxntppoolxx` and `date xxx` – Waygood May 16 '13 at 09:24
  • If you can convince me there are no such functions in PHP4, and the NTP solution is simpler than to code something directly, I will give it a try. But actually I cannot use NTP. When I want to to this correctly, I need a php4 function, and ONLY php4 (without any other package, service, tool, code, ...) – Alex May 16 '13 at 09:26

1 Answers1

0

Supposing a Linux system is used, one can use the date function to set the date directly. One then has

$aDate = getdate();
...
do something else
...
exec("/bin/date ".sprintf("%02d",$aDate["mon"]).sprintf("%02d",$aDate["mday"]).
        sprintf("%02d",$aDate["hours"]).sprintf("%02d",$aDate["minutes"]).sprintf("%04d",$aDate["year"]).
        ".".sprintf("%02d",$aDate["seconds"]));

If the date is changed in-between, this does not matter as it will be set back to the time before the intermediate steps.

Remarks:

  • The inter-between processing should not take too much time, as the time afterwards will be wrong. If the processing is less than a second, this should be fine
  • Although the use of exec poses a security risk, the above usage is safe IF AND ONLY IF the variable $aDate is not tampered with inbetween.
Alex
  • 41,580
  • 88
  • 260
  • 469