1

Ho do I convert:

2010-12-24 11:39:43

to:

24/12 11:39

Thanks.

walter
  • 111
  • 1
  • 1
  • 6
  • 1
    To parse the input time: http://php.net/manual/en/function.strtotime.php – Pekka Dec 28 '10 at 10:07
  • 1
    To format the output: http://php.net/manual/en/function.date.php – Pekka Dec 28 '10 at 10:07
  • 1
    possible duplicate of [PHP date conversion](http://stackoverflow.com/questions/4544502/php-date-conversion). Please do not ignore http://stackoverflow.com/questions/ask-advice – Gordon Dec 28 '10 at 10:50
  • @Gordon - in the question you link to, you have a comment which says that it is a possible duplicate also, of http://stackoverflow.com/questions/2167916/php-convert-one-date-into-another-date-format, which may have been the one you meant all along. – Ben Mar 08 '11 at 04:45

4 Answers4

5

This should to the trick:

$newFormat = Date ( 'd/m H:i', StrToTime ( '2010-12-24 11:39:43' ) );

You use StrToTime to convert a string representation of a date to timestamp. You then feed that timestamp to the Date function that takes the format of the date as the first parameter.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • 1
    Why does the contribution with the most explanation have the least upvotes? The mysteries of voting... :) – Pekka Dec 28 '10 at 10:11
4

Try:

$unixtime = strtotime("2010-12-24 11:39:43");

$newFormat = date("d/m H:i", $unixtime);
3
echo date("d/m H:i", strtotime("2010-12-24 11:39:43"));
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
1
$date = DateTime::createFromFormat('Y-m-d G:i:s', 2010-12-24 11:39:43); //You can simply tell DateTime accept your timestamp as is since PHP 5.3

echo $date->format('d/m G:i T'); //Will output what you wanted + Timezone Abbreviation (because of the T) 
Kevin_L22
  • 113
  • 7
  • ^In 5.3, you can pass your datetime as is using new DateTime rather than using the DateTime Static function I provided. I personally think that's easier than the two methods above; The way above showed you how to always get out of situation where you think you have a unique DateTime. – Kevin_L22 Aug 12 '12 at 07:18