views:

107

answers:

2

Hey i'm trying to display a date/time from a timestamp field using:

echo $date = date( "D F j", $row['date'] );

However it returns: Wed December 3 When it should return today: Monday July 13

Timestamp from the DB row is: 2009-07-13 04:16:31

Thanks.

+3  A: 

You should use

SELECT UNIX_TIMESTAMP(`date`) AS `date` FROM ...

This will give you the date in the correct format to pass in to PHP's date() function.

Edit:

SELECT *, UNIX_TIMESTAMP(`date`) AS `date` FROM songs WHERE date >= DATE_SUB( NOW( ) , INTERVAL 2 WEEK )

As a side note - it's best to avoid using NOW() in queries as it prevents them being cached - inject the date in you application instead.

Greg
Erm... How would I integrate that into my SELECT * query: SELECT * FROM songs WHERE date >= DATE_SUB( NOW( ) , INTERVAL 2 WEEK )
ian
Thanks on the NOW() I will change it.
ian
How would you write that query without NOW()?
Svish
Put the date in the query in your app: DATE_SUB('2009-07-13', INTERVAL 2 WEEK)
Greg
Could you do time(); also or $now = $time(); or $now = date(); and then: DATE_SUB( $now , INTERVAL 2 WEEK) ?
ian
mysql_query('SELECT ... DATE_SUB("' . date('Y-m-d') . '", INTERVAL 2 WEEK) ...');
Greg
+1  A: 

Also possible:

$date = date( "D F j", strtotime( $row['date'] ) );

But Greg's solution is better coding.

Jeroen
Ahhh. That looks familiar. I could not remember how I did it before. Thanks for the alternate method.
ian