...

View Full Version : Time Left



theory
06-24-2005, 05: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;

Thanks!

devinemke
06-24-2005, 05: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';
}
else
{
echo 'expired';
}

theory
06-24-2005, 07: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.

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

<?php
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum