colourtheweb
colourtheweb

Reputation: 767

php date format conversion from d/m/Y to date('D, j M Y H:i:s')

I'm trying to convert the date in php but not getting any success. Any help will be really appreciable.

My code:

$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>";
}

Actual Output:

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

Expected output:

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

Upvotes: 2

Views: 3696

Answers (3)

Dave Lo
Dave Lo

Reputation: 1

strtotime() takes most formats and turns it into a unix timestamp. However, it's going to mix up MM/DD/YYYY and DD/MM/YYYY.

There are a lot of solutions here... one is to just explode the datestamp, using the format you're expecting.

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

Upvotes: 0

user1646111
user1646111

Reputation:

try with this and see if it works:

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>";
}

EDIT:

<?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

Upvotes: 1

demonslayer
demonslayer

Reputation: 66

The strtotime() function takes in argument in the mm/dd/yyyy format

Refer here for the various date formats.

So use 03/06/2013 to get Wed, 6 Mar 2013 00:00:00

Upvotes: 1

Related Questions