Question

Possible Duplicate:
how to convert php date formats to GMT and vice versa?

In PHP, I have a string date like this:

May 21, 2012 07:23:15 GMT

or this

21 May 2012 07:23:15 GMT

I need to convert both the strings into something like this: 21-05-2012. (day-month-year)

Can PHP correctly parse both these strings into a day-month-year format?

Was it helpful?

Solution

Try This -

 $your_string = "21 May 2012 07:23:15 GMT";
 $dd = date("d-m-Y", strtotime($your_string));
 echo $dd;

OTHER TIPS

Have you tried the DateTime class?

date_default_timezone_set('Europe/Stockholm');
$date = new DateTime("May 21, 2012 07:23:15 GMT");
print $date->format('Y-m-d');

You could use strtotime() for this. Alternatively, if you know what format to expect, you can use strptime() to parse it, which would be preferred.

Try this

$str_date = "May 21, 2012 07:23:15 GMT";
$date = DateTime::createFromFormat('M d, Y H:i:s O', $str_date);
echo  $date->format('d-m-Y');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top