0

below is my php code.

<?php
    $date = "06 06 2015 6:05:00 am";
    $cdate = date("Y-m-d H:s:i",strtotime($date));
    echo $cdate;
?>

Output is

1970-01-01 01:00:00

i need to 2015-06-06 6:05:00

Thanks

Renish Khunt
  • 5,620
  • 18
  • 55
  • 92

4 Answers4

3

Try this

<?php
$date = "06 06 2015 6:05:00 am";
$datetime= \DateTime::createFromFormat("m d Y h:i:s a", $date);
$cdate = $datetime->format('Y-m-d H:s:i');
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20
2

You want DateTime::createFromFormat() (http://php.net/manual/en/datetime.createfromformat.php). Using that you can convert a string to a datetime any way you want to.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
1

If this is all you plan to do with dates, continue, otherwise, consider using a good date and time class like Carbon to do more with dates and generally save yourself a lot of effort.

$date = "06 06 2015 6:05:00 am";
$cdate = date_create_from_format('m d Y g:i:s a', $date);

This allows you to specify your own format using format strings. For the entire list of format strings see: http://php.net/manual/en/datetime.createfromformat.php

The resulting $cdate is a DateTime object and implements the DateTimeInterface (http://php.net/manual/en/class.datetimeinterface.php) so you can use the format() method to convert the output to any format you need.

For more on format(): http://php.net/manual/en/datetime.format.php

Rob_vH
  • 760
  • 8
  • 11
-1
$date = "06 06 2015 6:05:00 am";

this format is not correct because first date or month but it will first year with the match of strtotime "Y-m-d H:s:i" and "06 06 2015" this is with space but it will be "-" sign.

$date = "2015-06-06 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;
Jakir Hossain
  • 2,457
  • 18
  • 23