Reputation: 133
I'm writing a function that works out whether or not the time is between 9am and 5pm on a working day. It then tells you when the next working day starts (if currently out of business hours) based on whether today's business hours have ended or (if after midnight) are about to begin.
All is going well so far, and to create the most readable code I could’ve used strtotime()
-– but how do you test strtotime()
?
Is there a way to alter the system time temporarily for testing purposes?
Upvotes: 0
Views: 466
Reputation: 583
To test strtotime you can use http://www.teststrtotime.com (press enter to submit the form, the button seems broken for now).
Upvotes: 1
Reputation: 3989
You don't even need to use the second argument for 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 );
Upvotes: 0
Reputation: 7729
strtotime accepts a second argument that sets the time to whatever you want instead of the current time
int strtotime ( string $time [, int $now ] )
$time is the format to mimic
$now is the timestamp to generate the date from
if now is provided it overrides the current time.
e.g. to test 6pm on Monday March 30th no matter what time the program runs
$format = '2009-03-30 18:00'
$timestamp = mktime(18,0,0,3,30,2009);
strtotime($format,$timestamp);
Upvotes: 1
Reputation: 39926
why not just a unit test that has an array of strtotime() input values?
...
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") );
Upvotes: 0
Reputation: 74538
Just use strtotime()
's optional second argument, to give it a specific timestamp to use. If you don't supply the second argument, it uses the current time.
Upvotes: 3