Reputation: 489
I am trying to display dates in the European format (dd/mm/yyyy)
with strtotime
but it always returns 01/01/1970.
Here is my codeline :
echo "<p><h6>".date('d/m/Y', strtotime($row['DMT_DATE_DOCUMENT']))."</h6></p>";
In my database, the field is a varchar and records are formated like yyyy.mm.dd
I use the same codeline for another field that is formated like yyyy-mm-dd (varchar too) and it works fine.
Thanks for your help.
Upvotes: 5
Views: 29176
Reputation: 8411
Quoting from the strtotime page in the PHP manual.
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat()
when possible.
So in your case it should either be in format YYYY-MM-DD or d.m.y.
If you want to parse your custom format then use date_create_from_format
For example,
date_create_from_format('Y.m.d',$row['DMT_DATE_DOCUMENT'])
Upvotes: 4
Reputation: 16462
Since the format yyyy-mm-dd
works, try to replace .
with -
:
date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));
Upvotes: 4
Reputation: 106
Try with:
$date = date_parse_from_format("Y.m.d", $row['DMT_DATE_DOCUMENT']);
$time = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);
echo "<p><h6>".date('d/m/Y', $time)."</h6></p>";
(Using date_parse_from_format()
instead of strtotime()
)
Or just:
$date = date_parse_from_format("Y.m.d", $row['DMT_DATE_DOCUMENT']);
echo "<p><h6>{$date['day']}/{$date['month']}/{$date['year']}</h6></p>";
Upvotes: 3