2015-07-13 93 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");