How to tell if its a leap year with PHP







PHP is one of the most admired and popular server side scripting languages which are widely used for creating websites. With faster turn-around time, enhanced security and affordability, PHP has become preferred choice of the website developers.

PHP contains a useful date function which can be used to determine if the current year or a specific year is a leap year. This post looks at how to use the date function to do this.


Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform PHP related queries.

In this context, we shall look into how to work with PHP.


More information about PHP date() function ?

The date function works like this:

date('format string');
or
date('format string', $date);

Here, 'format string' is the string containing the date format specifiers. The optional $date parameter is a UNIX timestamp representation of the datetime. If it is omitted then the current datetime is used.

An example to show the current date in YYYY-MM-DD format would look like this:

echo date('Y-m-d');


How to use date() to work out if it’s a leap year ?

PHP has a number of formatting placeholders for the date() function. One of these is "L" which returns 1 if it’s a leap year, and 0 if it is not. 

For example, the code below would display 1 if it is a leap year:

echo date('L');

Similarly, the code below shows a loop looking at the years 2000 to 2010. It outputs the year, and then 'Yes' if it's a leap year or 'No' if it is not.

for($i = 2000; $i < 2011; $i++) {
    echo $i, ': ', (date('L', strtotime("$i-01-01")) ? 'Yes' : 'No'), '<br       />';
}

This would display the following output:

2000: Yes
2001: No
2002: No
2003: No
2004: Yes
2005: No
2006: No
2007: No
2008: Yes
2009: No
2010: No

Just a quick explanation about some of the bits of the code.

date('L', strtotime("$i-01-01"))

The above part will substitute the numbers 2000 to 2010 for the $i variable, thus making values 2000-01-01 to 2010-01-01. The strtotime() function converts this representation of the date into a UNIX timestamp.

TThe 'L' format tells date() to tell us whether or not it's a leap year.

((...) ? 'Yes' : 'No')

If the date() function returns true then it will return the first value after ?. If it returns false it will return the value after the :. 1 will return true, and 0 false, so we will echo out ‘Yes’ or ‘No’ instead of 1 or 0 which is much more human readable.


[Need urgent support to customize your Laravel Website? We are available to help you today. ]


Conclusion

This article will guide you on how to work with PHP date() function to work out if the current year, or a specific year, is a leap year or not. 

JavaScript is the client side scripting language and PHP is the server side scripting language. In #PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.

PHP #program to check if a year is leap year or not:

php #function year_check($my_year) { if ($my_year % 400 == 0) print("It is a leap year"); if ($my_year % 4 == 0) print("It is a leap year"); else if ($my_year % 100 == 0) print("It is not a leap year"); else print("It is not a leap year"); } $my_year = 2024; year_check($my_year); ?>


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com




Focus on your business, not your servers.

Click Here to Learn More


Recent Post