Posts Tagged ‘Preg’

The Next Millennium Time Bomb

Friday, April 3rd, 2009

Just been working on a booking system for holiday lets and stumbled on the next millennium time bomb – I was using php mktime to add days to a date and discovered that if you add 1 day to Jan 1st 2037 the result is correct, but if you add 1 day to April 1st 2038 you get a date back in 1968 (a good year – but not the right answer!).

If you want to check it out yourself the PHP code is included below:

Function FindDate($sDate,$iDirection,$sTarget) {
// $sDate format YYYY-MM-DD from MySQL table
// $idirection to go forward or back in time 
// $sTarget day of week we want to find
list($yyyy,$mm,$dd)=preg_split('/\W/',$sDate);
$i=0;
do {
	$timestamp=mktime(0, 0, 0, $mm, $dd+$i, $yyyy);
	$i=$i+$iDirection;
	$w=date('D',$timestamp);  // gives Mon - Sun
	echo ($i." ".$sDate." ".$w);
	echo (date('Y-m-d',$timestamp)."\n");
    } while ($w!=$sTarget && abs($i<7));
    
$r= date('Y-m-d',$timestamp);
echo ($r."\n");
return $r;

}