PHP check if date between two dates
Edit: use
<=
or>=
to count today's date.
This is the right answer for your code. Just use the strtotime() php function.
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Check the date between two dates
You are checking with ||
in your case you need to use &&
because you need date BETWEEN
$date1 = '20-08-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
echo "Inside Range";
}else {
echo "Out of Range";
}
Explanation:
Need to change your condition from if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3)))
to if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
It's also significantly easier if you're using DateTime objects:
$date1 = new DateTime('20-08-2017');
$date2 = new DateTime('-3 months');
$date3 = new DateTime('-6 months');
if($date1 < $date2 && $date1 > $date3) {
echo "Inside Range";
} else {
echo "Out of Range";
}
PHP check if today is in between two dates
Try this:
<?php
$now = new DateTime();
$startdate = new DateTime("2014-11-20");
$enddate = new DateTime("2015-01-20");
if($startdate <= $now && $now <= $enddate) {
echo "Yes";
}else{
echo "No";
}
?>
php how to check if date string in between dates
The function strtotime()
is what you need. It takes a dateable string and converts it into seconds elapsed from epoch.
In simple terms:
if (strtotime(str_replace("/", "-", "18/10/2018")) > strtotime(str_replace("/", "-", "20/10/2018")))
echo "Greater";
else
echo "Smaller";
Just a heads up:
strtotime("18-10-2018")
int(1539835200)
strtotime("20-10-2018")
int(1540008000)
You might need to replace the separator as:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
check if date is between two dates without year
You only wish to compare on the m
and d
annually, so only create a string to compare against that consists of m-d
. Then you can shorten your start and end dates to only be m-d
as well. Because you have formatted your dates from bigger unit to smaller unit (month then day), you can simply compare the dates as regular strings.
Code: (Demo)
$now = date('m-d');
$start_md = "12-06";
$end_md = "12-27";
if ($start_md <= $now && $now <= $end_md) {
echo "Christmas time";
} else {
echo "Not christmas time";
}
PHP if between 2 dates
if (($date >= $snowStart) && ($date <= $snowEnd))
{
// do this and that
}
PHP check if date between two dates
Try this code
function getTanggalPeriod($tglDelivered)
{
$data=explode("-",$tglDelivered);
$periode1 = date('Y-m-d',strtotime('First Saturday '.date('F o', @mktime(0,0,0, $data[1], 1, $data[0]))));
$periode11 = date('Y-m-d',strtotime($periode1.' +6 day'));
if (($tglDelivered >= $periode1) && ($tglDelivered <= $periode11))
{
echo $tanggal_periode="$periode1 | $periode11";
}
else
{
echo 'Not OK';
}
}
$tglDelivered = date('Y-m-d',strtotime("2015-07-10")); // Make sure of maintain the date format.
getTanggalPeriod($tglDelivered);
PHP check if datetime between two datetimes without looping
You can check times by using below code and for dates you can follow above link mentioned in the question.
$startTime = strtotime("7:00");
$endTime = strtotime("10:30");
$chkStartTime = strtotime("10:00");
$chkEndTime = strtotime("12:10");
if($chkStartTime > $startTime && $chkEndTime < $endTime)
{ #-> Check time is in between start and end time
echo "1 Time is in between start and end time";
}elseif(($chkStartTime > $startTime && $chkStartTime < $endTime) || ($chkEndTime > $startTime && $chkEndTime < $endTime))
{ #-> Check start or end time is in between start and end time
echo "2 ChK start or end Time is in between start and end time";
}elseif($chkStartTime==$startTime || $chkEndTime==$endTime)
{ #-> Check start or end time is at the border of start and end time
echo "3 ChK start or end Time is at the border of start and end time";
}elseif($startTime > $chkStartTime && $endTime < $chkEndTime)
{ #-> start and end time is in between the check start and end time.
echo "4 start and end Time is overlapping chk start and end time";
}
Check if Date is between two dates in mysql
Use between:
SELECT * FROM events
WHERE '2012-01-18' between start_date AND end_date
BTW: Take care of time part if start and end are datetime types
check a date between two dates - PHP
This way your script will be a bit more dynamic.
// the below snippet checks the date retrieved from database
// against fiscal periods between years 2000 and 2050 and return the
// valid dates
$endYear=2000;
while($endYear<=2050) {
$end = $endYear.'/06/30';
$endDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = $initDate->sub(new DateInterval('P1Y'))->add(new DateInterval('P1D'));
$ddb = '2016-09-27';
$dateFrodDB = DateTime::createFromFormat('Y-m-d', $ddb);
if ($dateFrodDB>=$initDate && $dateFrodDB<=$endDate)
{ echo "valid\n";
echo "\tStartDate->\"".$initDate->format("Y-m-d")."\"\n";
echo "\tEndDate->\"".$endDate->format("Y-m-d")."\"\n";
echo "\tDateFromDatabase->\"".$dateFrodDB->format("Y-m-d")."\"\n";
}
$endYear++;
}
/* output
valid
StartDate->"2016-07-01"
EndDate->"2017-06-30"
DateFromDatabase->"2016-09-27"
*/
check this on PHP Sandbox
Related Topics
PHP Multidimensional Array Searching (Find Key by Specific Value)
How to Query Between Two Dates Using Laravel and Eloquent
PHP - Accessing Multidimensional Array Values
Cannot Use Object of Type Stdclass as Array
Generating a Random Password in PHP
How to Start and End Transaction in MySQLi
Json_Decode Returns Null After Webservice Call
How to Generate All Permutations of a String in PHP
Encrypt and Decrypt Text With Rsa in PHP
PHP: Replace Umlauts With Closest 7-Bit Ascii Equivalent in an Utf-8 String
PHP, How to Catch a Division by Zero
What's the Difference Between Isset() and Array_Key_Exists()
Laravel 5.5 Ajax Call 419 (Unknown Status)
How to Get the Error Message For the Mail() Function