0

我有一些 PHP4 代码,我需要在执行一些代码后恢复系统时间。我正在寻找一个简单的语句,我可以用它来存储当前日期/时间,然后再恢复它:

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

在 PHP5 中你可以很直接地使用date_timestamp_get()and date_timestamp_set(),但是如果你必须使用 PHP4 怎么做到这一点最简单呢?

PS 没有关于我为什么使用 PHP4 的问题。我也不喜欢...

4

1 回答 1

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.
于 2013-05-16T12:32:08.537 回答