ian
ian

Reputation: 12335

problem with date()

I am using the date() function to display a timestamp from my databse.

$date = date( 'F jS', $news_items['date']);

I know the $news_items['date']; as they return in a YYYY-MM-DD 00:00:00 format.

But after the function call $date dispays as December 31st for all values.

Upvotes: 2

Views: 463

Answers (3)

Majid Fouladpour
Majid Fouladpour

Reputation: 30242

The correct syntax for date function is given in PHP manual as:

string date  ( string $format  [, int $timestamp  ] )

As you can see the second argument is expected to be an integer, but you are feeding the function with a string.

Upvotes: 2

Björn
Björn

Reputation: 29381

That's because date() wants a timestamp and not a string. You're better of with something like this;

$dt   = date_create($news_items['date']);
$date = date_format($dt, 'F jS');

Or in the object oriented way;

$dt   = new DateTime($news_items['date']);
$date = $dt->format('F jS');

Upvotes: 3

brettkelly
brettkelly

Reputation: 28205

$date = date('F jS', strtotime($news_items['date']));

Try that :)

Upvotes: 7

Related Questions