Displaying Date and Time

I know that this is probably the most asked question in php
I am getting a weird result when running this script
This is what it is showing (partial):

01-01-1970 01:00:00

This is the script that I am running:


$raw = "Mon Apr 7 06:29:15 +0000 2014";

$xplod = explode(' ', $raw);

print_r($xplod);

$string = "$xplod[2]-$xplod[1]-$xplod[5]-$xplod[3]";

echo "<br /> $string";

$date = Date("d-m-Y H:i:s", strtotime($string));

echo "<br />$date";

I can’t figure where I am going wrong, can anyone tell me? The point of this is that I can insert dates and times into a MySQL database

Try

$string = "{$xplod[2]}-{$xplod[1]}-{$xplod[5]} {$xplod[3]}";

You can also use the DateTime class to do the conversion:


$raw = "Mon Apr 7 06:29:15 +0000 2014";
$date = new DateTime($raw);

echo $date->format("d-m-Y H:i:s");
// 07-04-2014 06:29:15

I checked the online PHP Manual and an English date is expected with spaces as seperators:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

echo strtotime(“10 September 2000”), "
";


<?php 
echo '<pre style="width:90%; margin:auto; background-color:#ff9; color:#000">';  

  $raw = "Mon Apr 7 06:29:15 +0000 2014"; 

  $xplod = explode(' ', $raw); 

  print_r($xplod); 

  $string = "$xplod[2]-$xplod[1]-$xplod[5]-$xplod[3]"; 
  $string = "$xplod[2] $xplod[1] $xplod[5] $xplod[3]"; 

  echo "<br /> $string"; 
  $stt = strtotime($string);
  if($stt)
  {
    echo '<br />Good ',$stt;
  }else{
    echo '<br />Bad Date format';
  }

  $date = Date("d-m-Y H:i:s", strtotime($string)); 
  echo "<br />$date"; 

  echo "<br />"; 
echo '</pre>';
?>  


// Output

Array
(
[0] => Mon
[1] => Apr
[2] => 7
[3] => 06:29:15
[4] => +0000
[5] => 2014
)

7 Apr 2014 06:29:15
Good 1396844955
07-04-2014 06:29:15

OK, thanks that has worked a treat!!!

Nice explanation John. At least we can pat each other on the back. Hee hee

Yes all three of us deserve a pat on the back for supplying three valid answers in less than an hour…

but you done very well to be the first :slight_smile:

I did try them but none of them worked for me, though thanks for the effort

As John and I both posted you need the space between date and time.

Even using your posted code (with the space)

<?php
$raw = "Mon Apr 7 06:29:15 +0000 2014";

$xplod = explode(' ', $raw);

print_r($xplod);

$string = "$xplod[2]-$xplod[1]-$xplod[5] $xplod[3]";

echo "<br /> $string";

$date = Date("d-m-Y H:i:s", strtotime($string));

echo "<br />$date";
?>

Returns

Array ( [0] => Mon [1] => Apr [2] => 7 [3] => 06:29:15 [4] => +0000 [5] => 2014 )
7-Apr-2014 06:29:15
07-04-2014 06:29:15 

Please echo date_timezone_set(); and let us know the results.