views:

215

answers:

5

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?

+3  A: 

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.

Chad Birch
Feeling foolish now - should have read the manual more thoroughly!
Rudenoise
A: 

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") );
Scott Evernden
+1  A: 

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);
Fire Crow
+1  A: 

Check out this page, it's all you need. :)

A: 

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 );
KOGI