开发者

How to change local time for testing purposes?

I'm writing a script, that is based on the current time and it wor开发者_运维问答ks fine, but I don't want to wait, so I can see, if the script works tomorrow, in 2, 3, 4 days, or a year.

Is there a way to trick the apache server (using php preferred) into thinking that it's a different time? :-)

And if so, is it easy to fix back withnout any perpament consequences?


I presume at some level inside your script you're using the time function to get the current time? (If you're not, you most likely should be.)

If so, simply replace the call to time with a call to mktime, set to the appropriate date/time in the future.

Worse case scenario (if you're not using a PHP script), could just simply change the current system date/time using the system "date" command ("man date" for the details) if you're on Unix/Linux use or the appropriate control panel/system setting, etc. if you're on Windows/Mac OS X, etc. However, I'd really not recommend doing this unless you're using a private test server.

Additionally, as I said in my comment, having to change the system clock is a poor solution - you really need to abstract the current time in whatever script you're using.


Agree with middaparka - changing the local time is all well and good if you're running your script locally, but what about when it's running on a server because it depends on a data/session/something else?

The answer is to just set a variable at the top of your script called $time and set it to time(), then use $time throughout your script instead of time(). Then, when you want to change the time for testing purposes, you only need to change that one line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜