Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : strtotime() returns always 01/01/1970

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.

like image 988
HerrM Avatar asked Jun 08 '12 09:06

HerrM


People also ask

What does Strtotime return in PHP?

PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

How do I convert Strtotime to date format?

Code for converting a string to dateTime $input = '06/10/2011 19:00:02' ; $date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

How do you add days in Strtotime?

For a very basic fix based on your code: $day='2010-01-23'; // add 7 days to the date above $NewDate = date('Y-m-d', strtotime($day .

How do you add hours on Strtotime?

You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');


2 Answers

Since the format yyyy-mm-dd works, try to replace . with -:

date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));
like image 69
flowfree Avatar answered Sep 19 '22 16:09

flowfree


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>";
like image 34
fquffio Avatar answered Sep 19 '22 16:09

fquffio