function convertDT($TS) {
$TS = strtotime($TS);
$TS -= date("Z");
$newTS = date("Y-m-d\TH:i:s\Z", $TS);
return $newTS;
}
echo "Good: ".convertDT('2010-04-20 01:23:45')."\n";
echo "Bad: ".convertDT('2010-31-20 01:23:45')."\n";
The second date returns: 1969-12-31T23:00:00Z
Why? Should this error?
strtotime()returnsfalsewhen you give it an invalid timestamp.falseis equivalent to0if you use it in an integer context, so when you pass it to this line:You are effectively creating a date from a timestamp of
0. In terms of the UNIX epoch,0is January 1st, 1970, which is where you’re getting your end result from.Your best bet would be soemthing like this: