<?php
/**
* Find the number of days between today and a date in the future or in the past.
*
* @param int $year
* @param int $month
* @param int $day
* @param int $accuracy
*/
function numberOfDays($year, $month, $day, $accuracy = 0)
{
# Check for valid parameters #
if (!is_int($day) || $day < 1 || $day > 31)
{
printf('Wrong parameter for $day. It must be an integer between 1 and 31.');
exit();
}
if (!is_int($month) || $month < 1 || $month > 12)
{
printf('Wrong parameter for $month. It must be an integer between 1 and 12.');
exit();
}
if (!is_int($year) || $year < 0)
{
printf('Wrong parameter for $year. It must be a positive integer.');
exit();
}
if (!checkdate($month, $day, $year))
{
printf('Invalid date.');
exit();
}
if ($accuracy !== 0 && $accuracy !== 1)
{
printf('Wrong parameter for $accuracy. It must be either 1 or 0.');
exit();
}
if ($accuracy === 1)
{
echo 'The exact number of days between '.$today.' and '.$day.'-'.$month.'-'.$year.' are '.$result;
}
else
{
echo 'The number of days between '.$today.' and '.$day.'-'.$month.'-'.$year.' are '.(int)$result;
}
}
?>