tail Posted May 8, 2009 Share Posted May 8, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/ Share on other sites More sharing options...
The Little Guy Posted May 8, 2009 Share Posted May 8, 2009 if you use "%" it will give you what is left over after division is done... Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829226 Share on other sites More sharing options...
tail Posted May 8, 2009 Author Share Posted May 8, 2009 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'; Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829330 Share on other sites More sharing options...
The Little Guy Posted May 8, 2009 Share Posted May 8, 2009 yeah, see what it gives you. Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829377 Share on other sites More sharing options...
tail Posted May 8, 2009 Author Share Posted May 8, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829473 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 http://snippets.dzone.com/posts/show/3044 Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829532 Share on other sites More sharing options...
tail Posted May 8, 2009 Author Share Posted May 8, 2009 That seems like a little much to do what I'm trying to do. Is there any way to accomplish this task the way I am? Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829539 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829548 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829550 Share on other sites More sharing options...
tail Posted May 8, 2009 Author Share Posted May 8, 2009 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'; Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829616 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Instead of doing the math over and over, why not just subtract from time? Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829617 Share on other sites More sharing options...
tail Posted May 8, 2009 Author Share Posted May 8, 2009 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-829639 Share on other sites More sharing options...
tail Posted May 9, 2009 Author Share Posted May 9, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-830437 Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157308-counting-down-years-weeks-days-etc/#findComment-830454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.