在脚本中设置系统日期? [英] Set system date in script?
问题描述
PHP是否提供一种在脚本中设置系统日期的方法?
Does PHP provide a way to set the system date within a script?
即这样我就可以使 date('Y')
返回 1999
,即使它当前是 2021
.
i.e. so that I can make date('Y')
return 1999
even though it's currently 2021
.
我希望能够快速设置系统日期以进行调试/测试.
I want to be able to quickly set the system date for debugging/testing purposes.
也许系统日期"是是错误的短语.我只希望它在运行时针对同一脚本进行更改.
Perhaps "system date" is the wrong phrase. I only want it to be changed for the same script, whilst it's running.
推荐答案
操纵测试的服务器日期会导致许多其他问题.不要这样做!
Manipulating the server date for a test creates a host of other problems. Don't do it!
您始终可以在脚本中将时间戳记用作第二个参数.使用strtotime(),您可以测试任何日期的脚本.
You can always use a timestamp as a second parameter in your script. With strtotime() you can test the script with any date.
$ts = time();
$ts = strtotime('1999-01-01');
echo date('Y', $ts); //1999
测试后,使用strtotime注释掉该行,然后使用time()初始化时间戳.然后,日期('Y')的行为就像没有第二个参数一样.
After the test, the line is commented out with strtotime and the time stamp is then initialized with time(). The date ('Y') then behaves like without a second parameter.
$ts = time();
//$ts = strtotime('1999-01-01');
echo date('Y', $ts); //2021
这篇关于在脚本中设置系统日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!