Page 1 of 1

Showing how many days are left....

Posted: Fri Jan 02, 2004 10:19 pm
by Mr. Tech
I am wanting to show how many days are left until a certain time....

I am able to work it out in seconds but not days:

Code: Select all

<?php
$db_date = 'Dec 31, 2003'; 
$registered_date_timestamp = strtotime($db_date); 
$seconds = 100000;

$now = time(); 
$expired = $now - $seconds; 

if ($registered_date_timestamp > $expired) { 
$timeleft = $time - $registered_date_timestamp;
$timeleft = $seconds - $timeleft;
$timeleft = $timeleft.".1";
$timeleft = $timeleft / 60;
$timeleft = explode(".", $timeleft);
echo $timeleft[0]; // Displays how many minutes are left...
} else {
echo "Time is up!";
}
?>
Does anyone know how I can show how many days? I don't want it displaying something like 2.85 days.... That doesn't make sense with time...

Thanks

Posted: Fri Jan 02, 2004 10:43 pm
by microthick
http://lab.artlung.com/php/countdown/

Code: Select all

/*
   declare target date; source: http://us.imdb.com/ReleaseDates?0121766 ; 
  */
  $day   = 25;     // Day of the countdown
  $month = 5;      // Month of the countdown
  $year  = 2005;   // Year of the countdown
  $hour  = 12;     // Hour of the day (east coast time)
  $event = "Star Wars Episode III will be released in the USA"; //event

  $calculation = ((mktime ($hour,0,0,$month,$day,$year) - time(void))/3600);
  $hours = (int)$calculation;
  $days  = (int)($hours/24);
/*
  mktime() http://www.php.net/manual/en/function.mktime.php
  time()   http://www.php.net/manual/en/function.time.php
  (int)    http://www.php.net/manual/en/language.types.integer.php
*/
?>
<ul>
<li>The date is <?=(date ("l dS of F Y h:i:s A"));?>.</li>
<li>It is <?=$days?> days until <?=$event?>.</li>
<li>It is <?=$hours?> hours until <?=$event?>.</li>
</ul>

Posted: Fri Jan 02, 2004 11:22 pm
by Mr. Tech
That's a good script but how would I do it with time()?

How do I make it so that it can be done automatically because I won't know what day it will finish... just the starting day..

Thanks

Posted: Sat Jan 03, 2004 9:51 am
by JAM

Code: Select all

<?php
$db_date = 'Jan 08, 2004';
$x = strtotime($db_date) - time();
$thetime = strftime("%d %H:%M;%S", $x);
if (!$thetime) { echo 'Date passed!';} 
else {
 $thetime = explode(' ',$thetime);
 echo 'Days left: '.$thetime[0].' - Time left: '.$thetime[1];
}
?>
?

Posted: Tue Jan 06, 2004 3:36 am
by Mr. Tech
Works! Thank you!