2015-07-13 90 views
0

我与代表此格式的时间戳的字符串工作:PHP将字符串转换成timestamp

Mon Jul 13 11:32:00 EST 2015 

我现在需要将其转换成时间戳插入到我的数据库的格式如下:

​​

我通常会用这个:

date("m/d/Y h:i:s A", $timestamp) 

,但是这是行不通的。我不需要担心时区,因为时区已经在当地时区

回答

0

您需要首先将时间戳字符串转换为时间。如果你想忽略的时区,你可以这样做:

// Set the time 
$time = "Mon Jul 13 11:32:00 EST 2015"; 
// Cut out the timezone details 
$time = str_replace(" EST", "", $time); 
// Convert the string time into a time and then into a timestamp 
$timestamp = date("m/d/Y h:i:s A", strtotime($time)); 
// echo $timestamp = "07/13/2015 11:32:00 AM" 

或者,您也可以使用像mktime()(http://php.net/mktime),但是这需要你分手了你的时间字符串转换成单独的元素。

0

只需两行代码。使用Datetime对象将使您的代码更清洁。

<?php 
$datetime = new Datetime("Mon Jul 13 11:32:00 EST 2015"); 
print $datetime->format("m/d/Y h:i:s A");