php日期格式从d / m / Y转换为日期('D,j M Y H:i:s')

时间:2013-03-28 17:08:18

标签: php datetime

我正在尝试在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++){
    $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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

strtotime()函数接受mm / dd / yyyy格式的参数

请参阅here了解各种日期格式。

因此请使用03/06/2013获取2013年3月6日星期三00:00:00

答案 2 :(得分: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
}