-4

I have an issue with displying a date in a certain format. The date is stored in my database like that :"01092014" how can I convert it to "09/01/2014" in PHP ? Thanks.

OussamaLord
  • 1,073
  • 5
  • 28
  • 39
  • 1
    Show us what you've tried. Also, stop storing your dates as useless strings. And that Jan 9th? Or Sept 1st? – John Conde Jan 16 '14 at 02:18
  • Is it stored as an integer or a string? – Dai Jan 16 '14 at 02:18
  • it is stored as a string and it is january 9. – OussamaLord Jan 16 '14 at 02:21
  • 1
    please use one of the proper date field types (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html - if using mysql) –  Jan 16 '14 at 02:22
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Glavić Jan 16 '14 at 08:25

1 Answers1

2
$dt = DateTime::createFromFormat('01092014', 'dmY');
echo $dt->format('m/d/Y');

If you're using PHP < 5.3 you'll want to consider doing the conversion in the SQL query:

SELECT STR_TO_DATE(datecol, "%d%M%Y") as datecol

The ugliest one-liner ever:

echo date('m/d/Y', mktime(0, 0, 0, substr('01092014', 0, 2), substr('01092014', 2, 2), substr('01092014', -4)));

See it in action

John Conde
  • 217,595
  • 99
  • 455
  • 496