|
|
|
|
|
|
| |
| <?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();
}
$now = time();
$today = date('d-m-Y', $now);
$result = (mktime(0, 0, 0, $month, $day, $year) - $now) / 86400;
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;
}
}
?>
<html>
<head>
</head>
<body>
<?php
echo numberOfDays(2006, 12, 31);
echo '<br>';
echo numberOfDays(2005, 12, 31, 1);
echo '<br>';
echo numberOfDays(2006, 1, 31);
echo '<br>';
echo numberOfDays(2005, 12, 31);
?>
</body>
</html> | | |
|
| Find the day of the week for any given year/month/day. Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides | | | Find if a year is leap. Categories : PHP, Date Time, Beginner Guides, Data Validation | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | | | Validating a URL with preg_match Categories : PHP, Regexps, Beginner Guides, Data Validation | | | Kewl Date Example Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides | | | Form Validation Using PHP to highlight non valid fields Categories : PHP, Form Processing, Data Validation, Beginner Guides | | | Validator - A PHP class that can can be used for validating Email IDs and Dates Categories : PHP, PHP Classes, Data Validation, Email, Date Time | | | Local Time clock and Server time usign PHP and JavaScript Categories : PHP, Java Script, Date Time, Beginner Guides | | | Change the background color of a website daily dynamically using the php date function to get the current day of the week and depending on that day, set the background color for the web page. Categories : PHP, Date Time, Beginner Guides, Web Design | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | Script loading time Categories : PHP, Beginner Guides, Date Time | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | PHP based Contact email form with multiple recipients, text file based, supports departments. Categories : PHP, Email, Beginner Guides, Filesystem | |
|
|
|