Jump to content

Archived

This topic is now archived and is closed to further replies.

scrupul0us

Countdown till date

Recommended Posts

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

Share this post


Link to post
Share on other sites
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)

[code]
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"
[/code]

hope that helps

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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
[code]
<?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"));

?>
[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.