คำนวณปีอธิกสุรทิน PHP leap year calculation


หน้าแรก PHP MySQL เกร็ดความรู้ คำนวณปีอธิกสุรทิน PHP leap year calculation


  Code
<? echo date("L");?>


หากแสดงผลออกมาเป็น 1 คือ Leap year
หากแสดงผลออกมาเป็น 0 คือไม่เป็น Leap year

หรือดูวิธีการเขียนของฝรั่งที่
A simple little function to calculate whether a specified year is a leap year or not:


  Code
function isleapyear($year = '') {
if (empty($year)) {
$year = date('Y');
}
$year = (int) $year;
if ($year % 4 == 0) {
if ($year % 100 == 0) {
return ($year % 400 == 0);
} else {
return true;
}
} else {
return false;
}
}



The algorithm applied above, translated to everyday language, is something like this:

If the year is divisible by 100, then it's not, unless the year is divisible by 400, when it is. Otherwise, if the year is divisible by 4, then it is. Otherwise, it's not.

Did you get all that? I know, it's probably easier to understand by looking at the code.

refer: http://lutrov.com/blog/php-leap-year-calculation

ขึ้นไปด้านบน