Jump to content

Recommended Posts

I'm trying to make a countdown to show the amount of time between two dates, however when the script starts calculating days it screws up and says the actual amount of days between the two and not the days leftover after calculating years and weeks. Here is my code:

 

$years_left = floor($difference/60/60/24/365).' years';
$weeks_left = floor(($difference - $years_left*60*60*24*365)/60/60/24/7).' weeks';
$days_left = floor(($difference - $weeks_left*60*60*24*7)/60/60/24).' days';
$hours_left = floor(($difference - $days_left*60*60*24)/60/60).' hours';
$minutes_left = floor(($difference - $hours_left*60*60)/60).' minutes';

Any help is much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/
Share on other sites

I'm not sure I understand what you're saying. I've never used modulus before. Should I replace the division signs with a % like this:

 

$years_left = floor($difference%60%60%24%365).' years';
$weeks_left = floor(($difference - $years_left*60*60*24*365)%60%60%24%7).' weeks';
$days_left = floor(($difference - $weeks_left*60*60*24*7)%60%60%24).' days';
$hours_left = floor(($difference - $days_left*60*60*24)%60%60).' hours';
$minutes_left = floor(($difference - $hours_left*60*60)%60).' minutes';

$difference = (1241746582+34667326)-time();
$years_left = floor($difference%60%60%24%365).' years';
$weeks_left = floor(($difference - $years_left*60*60*24*365)%60%60%24%7).' weeks';
$days_left = floor(($difference - $weeks_left*60*60*24*7)%60%60%24).' days';
$hours_left = floor(($difference - $days_left*60*60*24)%60%60).' hours';
$minutes_left = floor(($difference - $hours_left*60*60)%60).' minutes';

 

output: 13 years -2 weeks 13 days 13 hours 13 minutes

 

it returns a different output each time.

Sorry I did miss the "between" part.

 

http://www.pakiguys.com/php/find-time-between-two-dates-in-php.html

 

Maybe more of the function for you. As far as it being a little much, I think you are underestimating what you are wanting to do.

Talk about confusing! Please use mktime. It's better than hard coding those huge numbers.

 

Why do you have weeks but *not* months? That's stupid.

 

Your math is a bit off. Since years have no restrictions, calculate how many years it would take. Then subtract the time from $difference. Next, calculate the number of weeks, but remember that weeks can never be more than 52 because that would equal a year. Though if you did it right, you should not get 52. For days, it should be same math except you have to restrict it down to only 6 days or less because more than that would be a week. Get it?

 

I suggest you start over from a fresh mind. You're too caught up to think straight with that messy glob.

Thanks for the help everyone. And for the idea of using mktime(), ken2k7.

 

New code:

$until = $r['time']+$r['length'];
$time = mktime(date('G',$until),date('i',$until),date('s',$until),date('n',$until),date('j',$until),date('Y',$until)) - time();
$years = floor($time/31536000).' years,';
$months = floor(($time-($years*31536000))/2628000).' months,';
$weeks = floor(($time-($years*31536000)-($months*2628000))/604800).' weeks';
$days = floor(($time-($years*31536000)-($months*2628000)-($weeks*604800))/86400).' days';
$hours = floor(($time-($years*31536000)-($months*2628000)-($weeks*604800)-($days*86400))/3600).' hours';
$minutes = floor(($time-($years*31536000)-($months*2628000)-($weeks*604800)-($days*86400)-($hours*3600))/60).' minutes';

Look at your code. You have a bunch of $years * 31536000 and $months * 2628000 etc in your math. You're making the computer re-calculate those values every time. It does it fast, but I think it's better to just save that in a variable so it doesn't have to keep multiplying the same numbers over and over again. Same for the subtraction. Get it now?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.