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
2010-02-26 12:32:43
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
2010-02-26 12:39:43
+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
2010-02-26 12:33:03
+2
A:
if (($year % 400 === 0) || (($year % 100 !== 0) && ($year % 4 === 0))) echo "leap";
Xr
2010-02-26 12:34:24
+12
A:
This is how to know it using one line of code :)
print (date("L") == 1) ? "Leap Year" : "Not Leap Year";
Sarfraz
2010-02-26 12:35:11
@Gordon - date("L") returns bool already. `$isLeapYear = date("L");` is all you need.
thetaiko
2010-02-26 13:21:43
@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
2010-02-26 13:50:33