dongsilu2237
2013-10-17 10:50
浏览 91
已采纳

strtotime()没有转换为时间戳

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

图片转代码服务由CSDN问答提供 功能建议

我有一个循环,其中日期以不同的格式出现,就像某些值一样,它会像 '10 -13-2013 04:31'有些人就像'2013-10-14T22:14:40-0700'。 我试图将它作为datetime / timestamp列的值存储在DB中,但是第一种格式 10-13-2013 04:31 失败了。 所以我尝试使用 strtotime()将其转换为UNIX时间戳。 它适用于某些值,并为 '10 -13-2013 04:31'等值存储零。 我认为这是因为它将第二个值视为月份,因此失败了。 我的代码如下:

  foreach($ reports as $ report){
 echo strtotime($ report-> transactionDate); 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshao1156 2013-10-17 10:57
    已采纳

    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');
    
    点赞 评论

相关推荐 更多相似问题