I have 2 date ranges
[contract_start_date] => 2011-10-20 [contract_end_date] => 2011-10-29
and I want to verify if the dates below are in range of the date range above
2011-05-05 and 2011-10-10
the dates given must not in any way exceed the range of the contract
Is there a function for this in PHP ?
The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
To check if a date is between two dates:Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.
PHP date() Function The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.
See:: http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime('2011-10-20');
$datetime2 = new DateTime('2011-10-29');
//PHP 5.3.0
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
//PHP 5.2.2
var_dump($datetime1 < $datetime2);
$datetime3 = new DateTime('2011-05-05');
$datetime4 = new DateTime('2011-10-10');
if ($datetime3 > $datetime1 && $datetime2 > $datetime1 && $datetime3 < $datetime2 && $datetime2 < $datetime2) {
//valid range
}//end if
This should give you exactly what you're looking for:
define(CONTRACT_START, "2011-10-20");
define(CONTRACT_END, "2011-10-29");
function checkDateRange($dateArray)
{
foreach($dateArray as $dateStr)
{
$curDate = strtotime($dateStr);
if($curDate < strtotime(CONTRACT_START) || $curDate > strtotime(CONTRACT_END))
{
return false;
}
}
return true;
}
$dates = array( 0 => "2011-10-02", 1 => "2011-10-25");
if(checkDateRange($dates))
{
echo "Dates are within range";
}
else
{
echo "Dates are NOT within range";
}
$start = strtorime($contract_start_date);
$end = strtotime($contract_end_date);
$required_start = strtotime("2011-05-05");
$required_end = strtotime("2011-10-10");
if ($end > $required_end or $end < $required_start)
{
//out of range
}
if ($start < $required_start or $start > $required_end)
{
//out of range
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With