This oneliner depends on the fact that PHP treats 0 as false and non-0 as true:
Given the variables $month and $year:
<?php
$daysInMonth = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31));
?>
$daysInMonth = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31));
?>
Here is the expanded version for clarity:
<?php
if ($month == 2) // Is it February?
{
if ($year % 4 != 0) // year not divisible by 4? This February has 28 days
{
$daysInMonth = 28;
}
else
{
if ($year % 100 != 0) // year not divisible by 100? This February has 29 days
{
$daysInMonth = 29;
}
else
{
if ($year % 400 != 0) // year is divisible by 100 but not by 400? This February has 28 days
{
$daysInMonth = 28;
}
else // year is divisble by 400
{
$daysInMonth = 29;
}
}
}
else
{
if (($month - 1) % 7 % 2) == 0)
{
$daysInMonth = 30;
}
else
{
$daysInMonth = 31;
}
}
?>
if ($month == 2) // Is it February?
{
if ($year % 4 != 0) // year not divisible by 4? This February has 28 days
{
$daysInMonth = 28;
}
else
{
if ($year % 100 != 0) // year not divisible by 100? This February has 29 days
{
$daysInMonth = 29;
}
else
{
if ($year % 400 != 0) // year is divisible by 100 but not by 400? This February has 28 days
{
$daysInMonth = 28;
}
else // year is divisble by 400
{
$daysInMonth = 29;
}
}
}
else
{
if (($month - 1) % 7 % 2) == 0)
{
$daysInMonth = 30;
}
else
{
$daysInMonth = 31;
}
}
?>