How can I format my timestamp right?

87 Views Asked by At

I have a timestamp from the mySQL database and I would like to format it.

$date = $row['date']; 
$date = date("F j, Y, g:i a"); 

My problem is now that it prints not the date of the database. Instead it prints out the current date. But this is not what I need.

So if I print this, I get the right date:

printf($row['date']);

But if I print this I get the right format but not the right date:

 printf($date);
2

There are 2 best solutions below

0
On BEST ANSWER

You need to pass that date, as a Unix timestamp, to date() as the second parameter. Otherwise date() has no idea you want to format a specific date:

$date = date("F j, Y, g:i a", strtotime($row['date'])); 

This assumes that $row['date'] is in a valid date format.

0
On

Use this as date() requires a timestamp:

$date = date("F j, Y, g:i a", strtotime($row['date']));

Otherwise, the time() function will be used for the second parameter and outputs the current time. $row['date'] should be either a PHP readable or a timestamp (in this case, you can leave strtotime() out).