2013-03-28 224 views
2

我試圖在php中轉換日期,但沒有收到任何成功。任何幫助都將非常可觀。php日期格式從d/m/Y轉換爲date('D,j M Y H:i:s')

我的代碼:

$playing_dates='06/03/2013|13/03/2013|20/03/2013|27/03/2013|03/04/2013|10/04/2013|17/04/2013|24/04/2013|01/05/2013|08/05/2013|15/05/2013|22/05/2013|29/05/2013'; 

$playing_dates = explode("|", $playing_dates); 
for($i=0; $i<count($playing_dates);$i++){ 
    $playing = strtotime($playing_dates[$i]); 
    echo $playing_dates[$i]." --> ".$playing." -->"; 
    echo date('D, j M Y H:i:s', $playing)."</br>"; 
} 

實際輸出:

06/03/2013 --> 1370214000 -->Mon, 3 Jun 2013 00:00:00 
13/03/2013 --> -->Thu, 1 Jan 1970 01:00:00 
20/03/2013 --> -->Thu, 1 Jan 1970 01:00:00 
27/03/2013 --> -->Thu, 1 Jan 1970 01:00:00 
03/04/2013 --> 1362355200 -->Mon, 4 Mar 2013 00:00:00 
10/04/2013 --> 1380841200 -->Fri, 4 Oct 2013 00:00:00 
17/04/2013 --> -->Thu, 1 Jan 1970 01:00:00 
24/04/2013 --> -->Thu, 1 Jan 1970 01:00:00 
01/05/2013 --> 1357344000 -->Sat, 5 Jan 2013 00:00:00 
08/05/2013 --> 1375657200 -->Mon, 5 Aug 2013 00:00:00 
15/05/2013 --> -->Thu, 1 Jan 1970 01:00:00 
22/05/2013 --> -->Thu, 1 Jan 1970 01:00:00 
29/05/2013 --> -->Thu, 1 Jan 1970 01:00:00 

預期輸出:

06/03/2013 --> 1370214000 -->Wed, 6 Mar 2013 00:00:00 
13/03/2013 --> -->Wed, 13 Mar 2013 00:00:00 
20/03/2013 --> -->Wed, 20 Mar 2013 00:00:00 
27/03/2013 --> -->Wed, 27 Mar 2013 00:00:00 
03/04/2013 --> 1362355200 -->Wed, 3 April 2013 00:00:00 
10/04/2013 --> 1380841200 -->Wed, 10 April 2013 00:00:00 
17/04/2013 --> -->Wed, 17 April 2013 00:00:00 
24/04/2013 --> -->Wed, 24 April 2013 00:00:00 
01/05/2013 --> 1357344000 -->Wed, 1 May 2013 00:00:00 
08/05/2013 --> 1375657200 -->Wed, 8 May 2013 00:00:00 
15/05/2013 --> -->Wed, 15 May 2013 00:00:00 
22/05/2013 --> -->Wed, 22 May 2013 00:00:00 
29/05/2013 --> -->Wed, 29 May 2013 00:00:00 
+2

這可以幫助你:http://stackoverflow.com/questions/5597833/strange-behaviour-of-my-code-with-strtotime/5597905#5597905 – LeonardChallis

+0

接近[PHP的strtotime完全相同的副本返回false](http://stackoverflow.com/questions/15687822/php-strtotime-returning-false)問不到一個小時前,並有全面的答案 –

回答

1

嘗試這一點,看看它是否工作:

for($i=0; $i<count($playing_dates);$i++){ 
    $date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]); 
    $newformat = $date->format('D, j M Y H:i:s'); 
    $playing = strtotime($date); 

    echo $playing_dates[$i]." --> ".$playing." -->"; 
    echo $newformat ."</br>"; 
} 

編輯:

<?php 
$playing_dates='06/03/2013|13/03/2013|20/03/2013|27/03/2013|03/04/2013|10/04/2013|17/04/2013|24/04/2013|01/05/2013|08/05/2013|15/05/2013|22/05/2013|29/05/2013'; 
$playing_dates = explode("|", $playing_dates); 
for($i=0; $i<count($playing_dates);$i++){ 
    $date = DateTime::createFromFormat('d/m/Y', $playing_dates[$i]); 
    $newformat = $date->format('D, j M Y H:i:s'); 
    $playing = strtotime($date->format("Y-m-d")); 

    echo $playing_dates[$i]." --> ".$playing." -->"; 
    echo $newformat ."</br>"; 
} 
?> 

06/03/2013 --> 1362524400 -->Wed, 6 Mar 2013 18:31:10 
13/03/2013 --> 1363129200 -->Wed, 13 Mar 2013 18:31:10 
20/03/2013 --> 1363734000 -->Wed, 20 Mar 2013 18:31:10 
27/03/2013 --> 1364338800 -->Wed, 27 Mar 2013 18:31:10 
03/04/2013 --> 1364940000 -->Wed, 3 Apr 2013 18:31:10 
10/04/2013 --> 1365544800 -->Wed, 10 Apr 2013 18:31:10 
17/04/2013 --> 1366149600 -->Wed, 17 Apr 2013 18:31:10 
24/04/2013 --> 1366754400 -->Wed, 24 Apr 2013 18:31:10 
01/05/2013 --> 1367359200 -->Wed, 1 May 2013 18:31:10 
08/05/2013 --> 1367964000 -->Wed, 8 May 2013 18:31:10 
15/05/2013 --> 1368568800 -->Wed, 15 May 2013 18:31:10 
22/05/2013 --> 1369173600 -->Wed, 22 May 2013 18:31:10 
29/05/2013 --> 1369778400 -->Wed, 29 May 2013 18:31:10 
+0

注意:PHP> = 5.3 – Sammitch

+0

另外請注意,應該有'!'作爲格式的第一個修飾符,將所有時間字段重置爲'0',然後[它會正常工作](http://viper-7.com/GijAtL)。 – nickb

+0

嘿阿克蘭非常感謝你的努力伴侶,但我恐怕這不是真的爲我工作。 – colourtheweb

1

的strtotime()函數採用在參數在毫米/ dd/yyyy格式

參見here用於各種日期格式。

因此,使用2013年3月6日獲得星期三,2013年3月6日00:00:00

0

strtotime()最需要的格式,並把它變成一個UNIX時間戳。但是,它會混合使用MM/DD/YYYYDD/MM/YYYY

這裏有很多解決方案......一個是使用您期望的格式來爆炸日期戳。

foreach($playingdates as $datestamp) { 
    list($day, $month, $year) = explode('/', $datestamp); 
    $playtime = mktime(0, 0, 0, $month, $day, $year); 
    // do whatever you want with $playtime 
} 
相關問題