How to change local time for testing purposes?

I am writing a script based on the current time, and it works fine, but I do not want to wait, so I see that the script works tomorrow, at 2, 3, 4 days or a year.

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

And if so, is it easy to fix it without any consequences?

+4
source share
2 answers

I assume that at some level inside your script, you use the time function to get the current time? (If you do not, you most likely should be.)

If so, just replace the time-based call with the mktime call, set the appropriate date / time in the future.

Worst case scenario (if you are not using a PHP script), you can simply change the current system date / time using the "date" command ("man date" for details) if you are using Unix / Linux or the corresponding control panel / system settings and etc. if you are running on Windows / Mac OS X etc. However, I would not recommend doing this if you are not using a closed test server.

In addition, as I said in my comment, changing the system clock is a bad decision - you really need to abstract the current time in any script that you use.

+4
source

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

The answer is simply to set a variable at the top of your script called $ time and set it to time (), and then use $ time during your script instead of time (). Then, when you want to change the testing time, you only need to change this line.

+1
source

Source: https://habr.com/ru/post/1336128/


All Articles