View Full Version : Time Left

06-24-2005, 06:09 PM
Alright, so entires on my site stay up for one week. So, I wish to tell the user how many days left until the entry expires. What I have now works, but I can see it causing major problems in the future. I need something that will be more precise. The date the entry was created is stored in the database.

$now = date('d',strtotime("now"));
$left = date('d',strtotime($date));
$left = $now - $left;
$left = 7 - $left;


06-24-2005, 06:56 PM
$date = '2005-06-20';

$timestamp_date = strtotime($date);
$timestamp_week_ago = strtotime('7 days ago 00:00:00');

if ($timestamp_date > $timestamp_week_ago)
$diff = $timestamp_date - $timestamp_week_ago;
echo $diff / 86400 . ' days left';
echo 'expired';

06-24-2005, 08:01 PM
Hrmmm, it seems to spit out a pretty long value, and for some reason - it also seems to be wrong.

For an entry with 7 days left, it says it has "Days of Voting Left: 7.5016898148148"

7.5 would be more than 7, maybe I'm doing something wrong.

06-24-2005, 11:26 PM
Are you storing the timestamp in the database? If so, try it this way:

$days = 7; // how many days to show for
$timepassed = time() - $date; // $date is the original date from your database, it should be a timestamp format
$dayspassed = ((($timepassed / 60) / 60) / 24);
if($dayspassed < $days)
echo ($days - floor($dayspassed)) . ' days left';
} else {
echo 'Time up';
?The floor() function should make sure that only whole days are shown so you won't get the 7.5016898148148 days problem.

If you're not storing the time but instead storing a date format, you will need to use strtotime to convert this first.