Showing how many days are left....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Showing how many days are left....

Post 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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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>
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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];
}
?>
?
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Works! Thank you!
Post Reply