user978905
user978905

Reputation: 5517

PHP MySQL - getting "days and hours" left of DATETIME

I have a MYSQL query that selects all rows that are less than 60 days old. I would like to display on my PHP page "x days and y hours left". What's the easiest way to do this?

SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(p.songid)
 FROM  songs s
 LEFT JOIN users u
 ON u.id = s.userid
 LEFT JOIN posttracks p
 ON s.songid = p.songid
 WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
 GROUP BY s.timeSubmitted DESC
 LIMIT 15

Upvotes: 0

Views: 691

Answers (2)

k.parnell
k.parnell

Reputation: 2787

In PHP try something along these lines when looping over your rows:

$then = strtotime($futureDateAsString);
$diff = $then - time();
echo sprintf("%s days and %s hours left", date('z', $diff), date('G', $diff));

Upvotes: 1

Marc B
Marc B

Reputation: 360762

Try TIMEDIFF().

Upvotes: 1

Related Questions