PHP date() Function Showing Date from 1970

December 30th, 2010 - Posted by Steve Marks to PHP, Web Development.

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'
Tags: ,
This entry was posted on Thursday, December 30th, 2010 at 12:14 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (3)
  1. 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? :(

  2. Craig says:

    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

  3. Shiv says:

    Nice Code It help me a lot