简体   繁体   中英

strtotime () not converting to timestamp

I have a loop and in it the date is coming in different formats like for some values it will be like '10-13-2013 04:31' and for some it is like '2013-10-14T22:14:40-0700' . I tried to store this in DB as the value of a datetime/timestamp column but it is failing for the first format that is 10-13-2013 04:31 . So I tried to convert it into UNIX timestamp using strtotime() . It is working for some values and is storing zero for values like '10-13-2013 04:31' . I think this is because it is considering the second value as month and so failing. My code is as follows :

foreach($reports as $report){
    echo strtotime($report->transactionDate);
}

strtotime() is unable to parse mm-dd-yyyy format. Instead you should use DateTime::createFromFormat() , like this:

$date = '10-13-2013 04:31';
$obj  = DateTime::createFromFormat('m-d-Y H:i', $date);
$date = $obj->format('Y-m-d H:i:s');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM