1

I extracted a date out of a MySQL database:

echo $nextup['date'] 

which is echoed as an DATETIME:

2014-03-19 15:21:42

Now, I'd like to convert/cast this to a readable form:

19 March 2014 15:21:42

but I couldn't find how. I've read the date/time manual for PHP but it doesn't say how to convert.

Thanks in advance!

M.G.Poirot
  • 1,066
  • 2
  • 11
  • 22
  • why not use mysql date_format() and convert the date at the time of selection unless u are using this date in regular format somewhere in the code http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format – Abhik Chakraborty Mar 19 '14 at 16:13

4 Answers4

4

Try strtotime and date combination:

function translate_names($eng) {
    $names = array (
      'Januari' => 'January',
      'Februari' => 'February',
      'Maart' => 'March',
      'April' => 'April',
      'Mei' => 'May',
      'Juni' => 'June',
      'Juli' => 'July',
      'Augustus' => 'August',
      'September' => 'September',
      'Oktober' => 'October',
      'November' => 'November',
      'December' => 'December',
);

    return array_search($eng, $names);
}

$date = $nextup['date'];
$month = addcslashes(translate_names(date('F', strtotime($date))), 'a..zA..Z');

$string = "d $month Y H:i:s";
echo date($string, strtotime($date));
aksu
  • 5,221
  • 5
  • 24
  • 39
  • You solved it, right away. Do you think it would be possible to chance March into another word/language. I'm working on a Dutch site so it'd be nice if the month would be in Dutch. – M.G.Poirot Mar 19 '14 at 16:18
  • I've found several sites with examples and as simple as it looks, nothing happens (March isn't translated) not in my version nor in the examples. I've tried different languages, no success. Could it be a problem that I'm working on localhost? – M.G.Poirot Mar 19 '14 at 16:33
  • 1
    Possibly, I'm making another example for you, wait a bit. – aksu Mar 19 '14 at 16:35
2

aksu was first, but for completeness, you could also do…

$dateTime = new DateTime($nextup['date']);
echo $dateTime->format("d F Y H:i:s");
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
1

echo date('d D Y H:i:s' , strtotime($nextup['date']));

This should work.

OfirH
  • 651
  • 1
  • 8
  • 19
1
$date = new DateTime("now");
echo date_format($date, "d F Y h:i:s");
oshell
  • 8,923
  • 1
  • 29
  • 47