Jump to content


Photo

days on countdown


  • Please log in to reply
6 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 September 2006 - 10:31 AM

How can i change a number to list years, months, weeks, days? e.g. "It is your birthday in 1year 2 months and 3 days" instead of it just showing 424days?

Thanks in advance

regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 10:46 AM

Is this for people born on Feb 29 who only have a birthday every 4 years? Otherwise the next birthday isn't going to be more than 365 days away.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 September 2006 - 12:01 PM

Haha

sorry that was bad example.. basicaly they can create countdowns for anything i just did the example to show like 1 year and months and weeks and days.. so basicaly need it to convert days into years, months, weeks and days.. if this is possible?


Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 paul2463

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

Posted 03 September 2006 - 12:21 PM

Hi shocker

Try this
<?PHP
function NoOfDays($Num){

   $years = 0;
   $months = 0;
   $weeks = 0;
   $days = 0;

   $test = floor($Num/365.25);
   if($test > 0){
   $years = $test;
   $difference = $Num - ($years*365.25);
   }

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

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

   }
   if ($difference >= 1){
   $days = $difference;
   }

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

   return $output;
}

echo NoOfDays(424);

?>
it works on the principle that each year has 365.25 days (to take into account the extra day every 4 years), every month has 30.4 days in it (to take into account that for 365.25 days divided by 12 months = 30.4), each week has seven days ( all weeks have seven days I know but...) and so on..

the above code entered with you posted number of 424 returns

You have to wait 1 Years, 1 Months, 4 Weeks, 0 Days until this Day

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

#5 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 September 2006 - 01:04 PM

only 1 minor problem with that.. it doesn't work if you input a date less than a year (365.25) Any mods that can be done? im stumped on this one..

Thanks for the help :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 September 2006 - 01:10 PM

JACKPOT :D

<?PHP
function NoOfDays($Num){

   $years = 0;
   $months = 0;
   $weeks = 0;
   $days = 0;
   $difference=$Num;

   $test = floor($Num/365.25);
   if($test > 0){
   $years = $test;
   $difference = $Num - ($years*365.25);
   }

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

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

   }
   if ($difference >= 1){
   $days = $difference;
   }

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

   return $output;
}

echo NoOfDays(424);

?>

I added $difference=$Num; as if no years it wasn't created so of course there is no difference

Thanks Paul :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 paul2463

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

Posted 03 September 2006 - 01:12 PM

   $days = floor($difference);
 

this part needs adjusting to or you get responses like 3.8 days in the printout

and you are welcome
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