2

我正在尝试在 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
4

3 回答 3

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
于 2013-03-28T17:16:34.913 回答
1

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

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

所以使用 03/06/2013 得到 Wed, 6 Mar 2013 00:00:00

于 2013-03-28T17:18:49.570 回答
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
}
于 2013-03-28T17:41:49.853 回答