views:

23

answers:

1

Ok, So i have a script that reads in a csv file and in there is a time that is formatted in the traditional HH:MM am/pm. I need to convert that into the mysql standard time format (HH:MM:SS).

This is what i have so far and it works

$schedule[$row]["TIME"] = date("H:i:s", strtotime($data[4]))

the problem is, if the input is formatted incorrectly there is no way i can tell. Is there some sort of "or die()" feature i can use or do i have to somehow check with a regex or something?

For example:

12:00 pm should be 12:00:00 but...if theres some issue with the format 12:d00 p.m comes out as 17:00:00

Thanks, Ian

A: 

strtotime returns false if the parsing fails. So you can do:

$t = strtotime($data[4]);
if ($t === false)
    //handle error
$schedule[$row]["TIME"] = date("H:i:s", $t);
Artefacto
Please be aware that in older 5.x versions of PHP, a failed `strtotime` can return `-1` instead of false.
Charles