题
我正在写出来的作品是否没有时间的函数是上午9点到下午5点在工作日之间。然后,它告诉你什么时候下一个工作日根据今天的营业时间是否已经结束或开始(如果目前不能营业时间)(如果午夜后)即将开始。
一切都进展顺利,到目前为止,并创造最可读的代码,我可能已经使用strtotime()
- 但你怎么测试strtotime()
有一种方法,以暂时改变系统时间用于测试目的?
解决方案
只要使用strtotime()
的第二个可选参数,给它一个特定的时间戳来使用。如果你不提供第二个参数,它采用了当前时间。
其他提示
的strtotime接受设置时间到任何你想要代替第二个参数的当前时间
INT的strtotime(字符串$时间[摘要$现在])
$时间的格式来模拟
$现在是生成从日期时间戳
如果现在提供它覆盖当前时间。
e.g。周一3月30日下午6点进行测试,无论在程序运行什么时间
$format = '2009-03-30 18:00'
$timestamp = mktime(18,0,0,3,30,2009);
strtotime($format,$timestamp);
请查看此页面,这是你所需要的。 :)
要测试的strtotime您可以使用 http://www.teststrtotime.com (按回车键提交表单时,按钮似乎破裂现在)。
为什么不只是具有的strtotime阵列的单元测试()的输入值?
...
test( strtotime("now") );
test( strtotime("10 September 2000") );
test( strtotime("+1 day") );
test( strtotime("+1 week") );
test( strtotime("+1 week 2 days 4 hours 2 seconds") );
test( strtotime("next Thursday") );
test( strtotime("last Monday") );
您甚至不需要使用为的strtotime)的第二个参数(;
// $now = time( ); // use this once testing is finished
$now = strtotime( '03/15/2009 14:53:09' ); // comment this line once testing is complete
testTime( $now );
不隶属于 StackOverflow