20
How do I know if the current year is a leap year in PHP?
20
How do I know if the current year is a leap year in PHP?
27
Another way is to use the function date
with the parameter L
returning 1
if you are in leap year, 0
otherwise.
Example for current year:
echo date('L');
For a specific year:
$ano = 2007;
$bissexto= date('L', mktime(0, 0, 0, 1, 1, $ano));
echo $ano . ' ' . ($bissexto? 'é' : 'não é') . ' um ano bissexto.';
See working on ideone.
20
Another way to know if the year is leap, is to know the amount of February days, this can be done with the argument t
of function date()
echo date('t', strtotime('2016-02-01')); //29
Congratulations, brilliant, brilliant sir. such little code, wow the best
.
Very good, really it is always good to remember the parameters of date
!! Thank you!
It’s @Guilhermenascimento. I think there’s a crowd that doesn’t know that :D
I really want to edit the answer and add all parameters :D
15
To do such an operation you can use two ways, but the two lead to the same method: Check whether February ends with day 29.
Strtotime and date
(date('d', strtotime('last day of february this year')) === '29')
Objeto Datetime
(new DateTime('last day of february this year'))->format('d') === '29')
This solution is so simple, that some questioned me in the chat if I was joking. To be convincing, I made an example at IDEONE:
It is possible to use this syntax for any year. If you wish to check another year, the passage could be changed this year
for 2018
or next year
for example.
last day of february this year
:D PHP "speaker" hehehe +1
15
An alternative solution is to use a native PHP function that is suitable for this, the cal_days_in_month
Examples:
<?php
//2015 não é bisexto
$result = cal_days_in_month(CAL_GREGORIAN, 2, 2015) === 29;
var_dump($result);
//2016 é bisexto
$result = cal_days_in_month(CAL_GREGORIAN, 2, 2016) === 29;
var_dump($result);
Online example: https://ideone.com/JgvjPQ
A simple function:
function isLeapYear($year = NULL) {
$year = is_numeric($year) ? $year : date('Y');
return cal_days_in_month(CAL_GREGORIAN, 2, $year) === 29;
}
Using:
var_dump(isLeapYear());//Ano atual
var_dump(isLeapYear(2015));//Ano 2015
var_dump(isLeapYear(2016));//Ano 2016
var_dump(isLeapYear(2017));//Ano 2017
Online: https://ideone.com/6ptj5P
This one I didn’t know, cool :D +1
7
We can use the rule of gregorian calendar, in which a leap year follows the following rules:
Then it would look like this:
$year = date('Y');
if (($year % 4 == 0) && ($year % 100 != 0 || $year %400 == 0)) {
echo "$year é bissexto";
} else {
echo "$year não é bissexto";
}
Heed: this method differs from some responses depending on the year. For example, comparing with the method of catching the last day of February:
for ($year = 1; $year < 10100; $year++) {
$x = ($year % 4 == 0) && ($year % 100 != 0 || $year %400 == 0);
$y = date('d', strtotime('last day of february '. $year)) === '29';
if ($x != $y) {
echo "$year, $x, $y\n";
}
}
This gives difference for years smaller than 1000 and larger than 10000.
Doing the same test above with cal_days_in_month
instead of strtotime
, the results do not differ. And with mktime
, only made a difference in the year 100.
It is worth remembering that this rule applies to the Gregorian Calendar, which is what we currently use in most of the world. So for practical purposes we can consider the above code and the other answers.
But if we consider other calendars, then the rule changes. For example, in Julian calendar, every 4 years a leap year occurs, so the way to calculate it would be simply if ($year % 4 == 0) { bissexto }
.
And things can get even more complicated if we want to consider which calendar was being used, depending on the year.
Officially the Gregorian Calendar was decreed in 1582. But, every place in the world adopted it on a different date, then if it’s to be needed at all, you’d need to consider not only when, but also where, to know which rule to apply (just to name a few examples, France made the change in 1582, while Turkey only changed in 1926). Of course if you will only work with current dates, this is not a concern.
And of course, there are still other calendars currently used, with completely different rules: the Jewish Calendar defines 7 leap years at every 19 year interval (see the calculation rule here), the Persian calendar does not use a mathematical rule and is based on the occurrence of equinoxes, etc.
But I think the question refers - implicitly - to the Gregorian Calendar, so this part of the answer is more of a curiosity.
0
In a row, you can check by formatting the date with 'L' (Leap year)
echo (date('L', strtotime("$ano-01-01")) ? 'SIM' : 'NÃO');
Where $ano
is the year you want to test.
See more parameters here
Browser other questions tagged php date
You are not signed in. Login or sign up in order to post.
+1. Yesterday I converted an old calendar system done in Datetime using date() + timestamp, and the code got 60% of the original size, and much simpler to read. And I have saved the information of the day as ( time() or timestamp / 86400 ) to simplify in DB when there is no use of hours (just do not forget the date('Z') to adjust the Timezone).
– Bacco