0

How to convert below date format into mysql date format for fetch data,

01:14:30 PM Wed 6th September 2017

My tried code is below,

$t= "01:14:30 PM Wed 6th September 2017";
echo date("Y/m/d H:i:s",strtotime($t));

but not get exact result, I get reult 2017/09/06 00:00:00 , I need to get exact result like 2017-09-06 13:14:30 How to get my exact result ,Any help appreciated

Thiyagu
  • 746
  • 3
  • 11
  • 29
  • 2
    you can use [DateTime::createFromFormat](http://php.net/manual/de/datetime.createfromformat.php) for such special formats to be converted to a date. – Jeff Sep 10 '17 at 15:34
  • You also aren't using the correct delimiters in your specified date format... use hyphens/dashes not slashes. – Devon Bessemer Sep 10 '17 at 15:36

3 Answers3

1

Working Demo: https://eval.in/859020

  <?php

//$Date = new DateTime("01:14:30 PM Wed 6th September 2017");
//$New_Date = date(Y-m-d h:i:s,strtotime(01:14:30 PM Wed 6th September 2017)); 
//echo $New_Date;

$Current_Date = "01:14:30 PM Wed 6th September 2017";

$date = DateTime::createFromFormat(' h:i:s A D dS M Y',$Current_Date);
echo $new_date_format = $date->format('Y-m-d H:i:s');

?>

Output: 2017-09-10 03:41:38

RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
-1

You are using / instead of - date string format.

For getting timestamp i.e. date with time use Y-m-d H:i:s string format:

echo date("Y-m-d H:i:s",strtotime($t));

For getting only date, use Y-m-d format:

echo date("Y-m-d",strtotime($t));
Deepansh Sachdeva
  • 1,168
  • 9
  • 16
-1

Just put the time at the end.

$t= "Wed 6th September 2017 01:14:30 PM ";
vladatr
  • 616
  • 6
  • 15