views:

273

answers:

5

How can I write a leap-year program in one line using PHP?

+1  A: 
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
Bastiaan Linders
Ah! I didn't know the meaning of leap-year, sorry. But I'll keep this "answer" here in case people end up here with the question that I thought you asked!
Bastiaan Linders
+3  A: 

Since there is no limit in how long a line of code is, unless you are using a code convention like that of Zend Framework, you can use whatever works and write into one line. Of course, depending on the functionality of your leap-year program, this will likely be hard to maintain. I've seen legacy code running over 800 chars with PHP, HTML and CSS intermingled. Eye-bleeding, I can tell you.

Gordon
+2  A: 

echo date("L");

Select0r
+2  A: 

if (($year % 400 === 0) || (($year % 100 !== 0) && ($year % 4 === 0))) echo "leap";

Xr
+12  A: 

This is how to know it using one line of code :)

print (date("L") == 1) ? "Leap Year" : "Not Leap Year";
Sarfraz
Even shorter: `$isLeapYear = (date("L") == 1);`
Gordon
@Gordon: That's true, but i just wanted to print the output :)
Sarfraz
Thanks for the help
streetparade
@streetparade: you are welcome :)
Sarfraz
@Gordon - date("L") returns bool already. `$isLeapYear = date("L");` is all you need.
thetaiko
@thetaiko Not bool. String. `var_dump(date('L')); // string(1) "0"`. But since 1 and 0 would be typecasted as needed, yes, I agree it can be shortened even further.
Gordon