chiprivers Posted December 31, 2007 Share Posted December 31, 2007 I just want to display on my site the number of days left til end 2008, could somebody give me a simple snippet? Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/ Share on other sites More sharing options...
asmith Posted December 31, 2007 Share Posted December 31, 2007 if you can get the point : $a = date('Y'); $b = 1900; echo $a - $b; if not, tell me. Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426543 Share on other sites More sharing options...
PHP_PhREEEk Posted December 31, 2007 Share Posted December 31, 2007 <?php $year = 2008; if ( !$days = getDaysLeft($year) ) { echo 'The year entered has already passed!'; } else { echo 'Today is ' . date("M jS, Y") . '. There are ' . $days . ' days left until the end of ' . $year . '.'; } function getDaysLeft($year) { if ( date("L", mktime(0, 0, 0, 1, 1, $year)) == 1 ) { $days = 366; } else { $days = 365; } if ( date("Y") < $year ) { $daysBehind = mktime(0, 0, 0, 1, 1, $year) - mktime(0, 0, 0, date("m"), date("d"), date("Y")); $additional = intval($daysBehind/86400); $days = $days + $additional; } elseif ( date("Y") > $year ) { return FALSE; } else { $days = $days - date("z"); } return $days; } ?> PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426585 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 A shorter way.. <?php $intYear = "2009"; $intTime = strtotime("{$intYear}-01-01"); $intToday = time(); $intDays = floor(($intTime-$intToday)/86400); print $intDays; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426596 Share on other sites More sharing options...
PHP_PhREEEk Posted December 31, 2007 Share Posted December 31, 2007 A shorter way.. <?php $intYear = "2009"; $intTime = strtotime("{$intYear}-01-01"); $intToday = time(); $intDays = floor(($intTime-$intToday)/86400); print $intDays; ?> I ran your script, and from today's date, your script says 366 days left until 2009, while mine states 367. Being that 2008 is a leap year, hence having 366 days instead of 365, and adding in today being 1 day until 2008 begins, I would have to conclude that 367 is the correct result, no? Unless I've missed something... I also created the function so that the OP can re-use it if he finds necessary, or someone else wanders in looking for a similar countdown function/script thingamajig. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426603 Share on other sites More sharing options...
GingerRobot Posted December 31, 2007 Share Posted December 31, 2007 You just need to roundup, rather than round down. If you rerun rajiv's code with January 1st 2008 as the finish date, it will show 0 days, when, of course, it should be 1. By rounding down, you don't count today. <?php $intYear = "2009"; $intTime = strtotime("{$intYear}-01-01"); $intToday = time(); $intDays = ceil(($intTime-$intToday)/86400); print $intDays; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426607 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 Yes GingerRobot is correct the problem is in the rounding... Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426611 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 here new an improved code <?php $intYear = "2008"; $intTime = strtotime("{$intYear}-01-01"); $intToday = time(); $intDays = ceil(($intTime-$intToday)/86400); print $intDays; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83824-very-simple-countdown-script-stumped/#findComment-426614 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.