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.
Asked
Active
Viewed 38 times
-4

OussamaLord
- 1,073
- 5
- 28
- 39
-
1Show 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
-
1please 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 Answers
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)));

John Conde
- 217,595
- 99
- 455
- 496
-
I get a "Call to undefined method DateTime::createfromformat()" using this – OussamaLord Jan 16 '14 at 02:27
-
-
That's another problem. I updated my answer to show you how to do it in SQL. (I think) it's simpler than the PHP code you'd need. – John Conde Jan 16 '14 at 02:34
-
actually it's not in a DB I just wanted to make it simple for you guys , it's a date added at the end of a csv file (slashes are not allowed) so my file has a name_date like : file1_09012014 – OussamaLord Jan 16 '14 at 02:35
-
1
-