-4

My code:

   echo "Strtotime : ";

   echo date("Y-m-d",strtotime('9 March, 2015'));

   echo "<br> mktime :  ";

   echo date("Y-m-d",mktime('9 March, 2015'));

   echo "<br> Normal :  ";

   echo date("Y-m-d",'9 March, 2015');

I need output 2015-03-09

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • Where does the string `9 March, 2015` come from - do you have any control over this, what other formats might you be receiving? – Steve Sep 23 '16 at 10:45
  • Please try looking at the manual for [`mktime()`](http://php.net/manual/en/function.mktime.php) Its nothing like your attempt at using it – RiggsFolly Sep 23 '16 at 10:47
  • Also look at the manual for [`strtotime()`](http://php.net/manual/en/function.strtotime.php) If you use the functions correctly they will work correctly – RiggsFolly Sep 23 '16 at 10:50
  • Better check the manual for [`date()`](http://php.net/manual/en/function.date.php) as well as you are about it – RiggsFolly Sep 23 '16 at 10:52

4 Answers4

1

You can use DateTime with date_create_from_format, if the structure of the String is always the same:

$d = '9 March, 2015';

echo date_create_from_format('d F, Y', $d)->format('Y-m-d');

See a running example: https://3v4l.org/s8EHi

danopz
  • 3,310
  • 5
  • 31
  • 42
0

if you want to show date - month - year you can apply this

<?php 
echo date('Y-m-d');
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

I strongly recommend using the DateTime class for this:

$date = DateTime::createFromFormat ("d M, Y", "9 march, 2015");
echo $date->format ("Y-m-d");

If you check the PHP manual for DateTime, you can get a whole lot of other really good tips on how to further take advantage of this class. Which makes working with dates in PHP a lot easier than with the old functions. ;)

ChristianF
  • 2,068
  • 9
  • 14
0

I think you should use this code to get exact date format.

$orig_date = "22/09/2016";

$date = str_replace ('/', '-', $orig_date);

echo date('Y-m-d', strtotime ($date));

See more date and time format example here