Jump to content


Photo

Countdown till date


  • Please log in to reply
3 replies to this topic

#1 scrupul0us

scrupul0us
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 16 August 2006 - 12:58 PM

So I've been putzing around with various scipts online to basically just tell me how many:
years months weeks days

are left between now and a given date

ive tried hotscripts and all that jazz but most are insanely bulky or just dont work

e.g... if my event date is August 11 2007 the output should be (based on today august 16 2006):

11 months 3 weeks 5 days

so far ive gotten nothing to work

#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 16 August 2006 - 01:13 PM

try this $until needs to be a timestamp and not a date though, see example at the end ( This is based on months only having 30 days though)

function formatetimestamp($until){

   $now = time();
   $difference = $until - $now;

   $months = floor($difference/2592000);
   $difference = $difference - ($months*2592000);

   $weeks = floor($difference/604800);
   $difference = $difference - ($weeks*604800);

   $days = floor($difference/86400);
   $difference = $difference - ($days*86400);

   $output = "You have to wait $months Months, $weeks Weeks, $days Days until this Day";

   return $output;

}

//int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

echo formatetimestamp(mktime(0,0,0,12,31,2006)); //output: e.g "You have to wait 5 Months 1 Week 5 Days until this Day"

hope that helps

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 scrupul0us

scrupul0us
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 16 August 2006 - 02:19 PM

yesi tried that from php.net but it did not work for me when i passed:

mktime(0,0,0,08,11,2007)

i got: 11 months 51 weeks 360 days
(based on yesterdays date)

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 16 August 2006 - 04:42 PM

Hi , Sorry about that maybe I should have checked it before posting it, I have modified it so it works now, here is the code, it takes a date string as the input now in the form of the example at the bottom again
<?PHP
function formatetimestamp($until){
   $months = 0;
   $weeks = 0;
   $days = 0;

   $now = strtotime("now");
   $difference = $until - $now;

   $test = floor($difference/2592000);
   if($test > 0){
   $months = $test;
   $difference = $difference - ($months*2592000);
   }

   $test = floor($difference/604800);
   if($test > 0){
   $weeks = $test;
   $difference = $difference - ($weeks*604800);
   }

   $test = floor($difference/86400);
   if ( $test > 0) {
   $days = $test + 1;
   $difference = $difference - ($days*86400);
   }

   $output = "You have to wait $months Months, $weeks Weeks, $days Days until this Day";

   return $output;

}

echo formatetimestamp(strtotime("31 august 2006")); 

?>

try it now

again it is not extremely accurate because is predicated on all months having 30 days, to keep it simple, it gives a good idea though
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users