shadiadiph Posted March 6, 2010 Share Posted March 6, 2010 Is there anyway in php to find out if the current year or previous years was a leap year in php? Link to comment Share on other sites More sharing options...
inversesoft123 Posted March 6, 2010 Share Posted March 6, 2010 Googled in 3 seconds http://www.electrictoolbox.com/leap-year-php/ Link to comment Share on other sites More sharing options...
shadiadiph Posted March 6, 2010 Author Share Posted March 6, 2010 so if some one enters year of birth 1970 a query to check should be siome thing like this??? if ($dobyear==date('L')); { print 'leap year'; } else { print 'not leap year'; } Link to comment Share on other sites More sharing options...
Psycho Posted March 6, 2010 Share Posted March 6, 2010 No, the date() function requires a timestamp - if you don't supply one then it assumes the current date/time. Here is a small function that you can pass a year value to to determine if it is a leap year (also included a loop for testing purposes): <?php function isLeap($year) { return (date('L', mktime(0, 0, 0, 1, 1, $year))==1); } //For testing for($year=1899; $year<2020; $year++) { If (isLeap($year)) { echo "$year : LEAP YEAR<br />\n"; } else { echo "$year : Not leap year<br />\n"; } } ?> Link to comment Share on other sites More sharing options...
shadiadiph Posted March 6, 2010 Author Share Posted March 6, 2010 thanks sorry just came back from the pub not too bright right now i shall test it tomorrow. Link to comment Share on other sites More sharing options...
shadiadiph Posted March 6, 2010 Author Share Posted March 6, 2010 actually that doesnt seem like what i am looking for eg somone enters a year of birth 1900 - current 2010 or more will that work? Link to comment Share on other sites More sharing options...
Zane Posted March 6, 2010 Share Posted March 6, 2010 eventhough the date() function comes equipped with a leapyear check.. You could just as easily divide the year by 4... and see if it returns a remainder... using the modulus operator. if(1776 % 4 == 0) echo "Christopher Columbus sailed the ocean on a leap year!"; Link to comment Share on other sites More sharing options...
Psycho Posted March 6, 2010 Share Posted March 6, 2010 actually that doesnt seem like what i am looking for eg somone enters a year of birth 1900 - current 2010 or more will that work? Yes, that function works for any year that you pass to it and returns a true or false based upon whether that year is a leap year. eventhough the date() function comes equipped with a leapyear check.. You could just as easily divide the year by 4... and see if it returns a remainder... using the modulus operator. if(1776 % 4 == 0) echo "Christopher Columbus sailed the ocean on a leap year!"; Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0)) After some more though on this I was a littel curious and decided to test what the performance difference was between determining leap year via the date() function vs. determining the leap year through mathmatical calculations. Although we are talking about milliseconds, the following functin is almost 100 times faster thant he function I posted above: function isLeap($year) { return ($year%4==0 && ($year%100!=0 || $year%400==0)); } Link to comment Share on other sites More sharing options...
Zane Posted March 6, 2010 Share Posted March 6, 2010 Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0)) Nice, I did not know this Link to comment Share on other sites More sharing options...
Psycho Posted March 6, 2010 Share Posted March 6, 2010 Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0)) Nice, I did not know this Yeah, I'm a bit of a perfectionist. Checking for divisible by 4 will work for any date from 1901 to 2099. So, if we are working with birth dates of people that are currently living they would have to be 110 years old for the rule to fail - pretty unlikely. But, since developers will commonly pick up existing code to reuse for other purposes and/or the OP could be dealing with a geaneology project it's better safe than sorry to do it exactly right. Link to comment Share on other sites More sharing options...
greatstar00 Posted March 6, 2010 Share Posted March 6, 2010 just do this $remainder=$currentYear MOD 4 if ($remainder==0) echo 'Leap year'; else echo 'NOT Leap year'; Link to comment Share on other sites More sharing options...
Mchl Posted March 6, 2010 Share Posted March 6, 2010 just do this $remainder=$currentYear MOD 4 if ($remainder==0) echo 'Leap year'; else echo 'NOT Leap year'; Except 1900 or 2100 are not Leap Years and your code will mark them as such. Link to comment Share on other sites More sharing options...
Psycho Posted March 7, 2010 Share Posted March 7, 2010 just do this $remainder=$currentYear MOD 4 if ($remainder==0) echo 'Leap year'; else echo 'NOT Leap year'; Yeah, not only did you fail to read the previous posts where a solution was provided (#3 & #7) your solution is flawed - for reasons stated in the previous posts. Link to comment Share on other sites More sharing options...
shadiadiph Posted March 7, 2010 Author Share Posted March 7, 2010 thanks mjdamato works great I have tested it with every leap and not leap year 1900 to current year. Link to comment Share on other sites More sharing options...
Mchl Posted March 7, 2010 Share Posted March 7, 2010 For PHP >= 5.3.0 you can use $d = DateTime::createFromFormat('Y',1900); echo $d->format('L'); // 0 Link to comment Share on other sites More sharing options...
Nateson Posted January 31, 2013 Share Posted January 31, 2013 <?php for($a=1913; $a<=2013; $a++) { if(date("z", mktime(0,0,0,12,31,$a))==365) { echo $a . "</br>"; } } ?> Just change the values of variable that is $a Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2013 Share Posted January 31, 2013 Since we are bumping this (already solved) thread - a solution using mktime/date has already been posted and it's been pointed out that using mktime/date doesn't work for people born before 1970 or 1901 (depending on php version and 32/64 bit operating system.) Topic locked... Link to comment Share on other sites More sharing options...
Recommended Posts