Date() function returning wrong date!

Hi dudes,

 I am passing the date function a "datetime" value which is pulled from my DB. The date field looks like this:
2010-04-16 04:45:32
2010-04-16 04:58:18
2010-04-16 06:02:17

And the PHP like this:

while($row = mysql_fetch_assoc($resourceID))
	echo date("D, M, Y", $row['date_created']);

And what I get output on screen is this:

Wed, Dec, 1969

As you can see there is no date in my table (shown above) with such an old date, any ideas what going on?

many thanks :wink:

That is a really helpful post thanks a million. I get very lazy when im not programming in Java, I should have checked the PHP manual first ! :slight_smile:

The second parameter to [fphp]date[/fphp] requires a unix timestamp (an integer), whereas you are passing it the date coming from the database (a string). You need to read the string and convert it to a timestamp first.

Luckily, a function exists to do that for you - [fphp]strtotime[/fphp].

This should solve the issue:

while($row = mysql_fetch_assoc($resourceID))
    echo date("D, M, Y", strtotime($row['date_created']));

Normally the code that you posted would output an error message explaining this to you; perhaps you have error reporting turned off on your server? If so, you might consider turning on error reporting for your development work (or writing your code on a development server with error reporting on before sending it to your production environment).

Tarh supplied the best answer, but depending on you needs your SQL select query could have turned the datetime to a unixtimestamp in your SQL statement, permitting you to skip that strtotime() step.


Or adding to what Cups said, have MySQL format the date itself. Remove PHP out of the equation entirely.