I’ve lost count of the amount of times I’ve used the PHP date() function and wondered why it’s returning a date from 1970 so I wanted to share with you the most common reason/mistake I’ve found for causing this in the event that you are facing the same problem.
The Solution
The date() function can accept two parameters; the format of the required date and an optional UNIX timestamp. If passing the second timestamp parameter remember that it must be converted to a timestamp. This can be done using the PHP strtotime() function. Let me show you an example below of my usual error with the date being pulled from a database and a corrected working version.
echo date("d F Y", $row['my_date']);
// Incorrect: Outputs '01 January 1970'
echo date("d F Y", strtotime($row['my_date']));
// Correct: Outputs the expected date. eg '30 December 2010'
Follow us on Twitter
Subscribe to RSS Feed
I have a similar problems but I’m not even passing a timestamp in. All I am calling is the following: date(“jS F Y”);
This works most of the time and returns todays date but occasionally, and usually on my clients machine, it returns : 1st January 1970
Anyone know why? :(
Such a simple thing, had me going for an hour. Strtotime(), won’t be forgetting that next time I use the date function. Good post – Cheers
Nice Code It help me a lot