开发者

Formatting String into MYSQL time

Ok, So i have a script that reads in a csv file and in there is a time that is formatted in the trad开发者_如何学运维itional 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜