I have to check if the incoming date is between 3 and 6 months before today. If it is outside this range, it has to execute certain code.
below is the code
<?php
$date1 = '22-10-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";
?>
For example if
Incoming date is 20-02-2018 - Out of Range.
Incoming date is 20-10-2017 - Inside Range.
Incoming date is 20-08-2017 - Out of Range.
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";
}
You can do like this:
$today=date_create(date("Y-m-d"));
$date=date_create("2018-06-12");
$diff=date_diff($today,$date)->format("%a");
if ($diff > 90 && $diff < 180) {
echo "Inside range";
}
else {
echo "Out of range";
}
Related
I want to check a certain date that is stored in my DB, if this date is during this fiscal year it prints valid if it is not it prints invalid
here is my php code:
$init_date= date("2016/07/01");
$end_date= date("2017/06/30");
$i_date = strtotime($init_date);
$e_date = strtotime($end_date);
$date_db= strtotime($date);// this $date is retrieved from my DB
if ($e_date > $date_db && $i_date < $date_db)
{ echo "valid";}
else { echo "invalid";}
but the problem is that i don't want to set the start and the end dates manually, is there a way to make it dynamic? as it will be updated every year
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
Try this,
<?php
$start_date = date('Y-m-d', strtotime(str_replace("/", "-", $initdate)));
$end_date = date('Y-m-d', strtotime(str_replace("/", "-", $end_date)));
$new_date = date('Y-m-d', strtotime(str_replace("/", "-", $date)));
if (($start_date < $new_date && $new_date < $end_date) || ($end_date < $new_date && $new_date < $start_date)) {
echo "valid";
} else {
echo 'invalid';
}
Here is working link
I have this code that says if days between 2 dates,
but i need it to work with Y-m instead of Y-m-d
how to achieve this?
example: i need to check if 2016-10 is between 2016-09 and 2016-12
my code:
$paymentDate = date('Y-m-d', strtotime($date));
$contractDateBegin = date('Y-m-d', strtotime($dfrom));
$contractDateEnd = date('Y-m-d', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Extract Month and Year and compare it.
$paymentDateY = date('Y', strtotime($date));
$paymentDateM = date('n', strtotime($date));
$contractDateBeginY = date('Y', strtotime($dfrom));
$contractDateBeginM = date('n', strtotime($dfrom));
$contractDateEndY = date('Y', strtotime($dtom));
$contractDateEndM = date('n', strtotime($dtom));
if ($paymentDateY >= $contractDateBeginY && $paymentDateY <= $contractDateEndY
&& $paymentDateM >= $contractDateBeginM && $paymentDateM <= $contractDateEndM){
echo "is between";
}
else
{
echo "NO GO!";
}
use Y-m is oK: Demo
$paymentDate=date('Y-m', strtotime($date));;
$contractDateBegin = date('Y-m', strtotime($dfrom));
$contractDateEnd = date('Y-m', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Hello i want to check if the current time is between two time range and calculate the difference between them, so far i have this but its not working
$current_time = "11:14 pm";
$start_time = "11:00 pm";
$end_time = "07:55 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time);
$date2 = DateTime::createFromFormat('H:i a', $start_time);
$date3 = DateTime::createFromFormat('H:i a', $end_time);
if ($date1 > $date2 && $date1 < $date3) {
echo 'in range';
} else {
echo 'not in range';
}
But it says "not in range"!
The main issue with your original code is that you are having it create dates from 3 times with unexpected results.
The start of the range is "11:00p" which it assumes means today at 11p.
The end of the range is "7:00a" which is assumes is also today. You actually intend to say "tomorrow at 7:00a".
You could try using strtotime.
$currentTime = strtotime("11:14 pm");
$rangeStart = strtotime("11:00 pm");
$rangeEnd = strtotime("tomorrow 07:55 am");
if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
echo 'in range';
} else {
echo 'not in range';
}
Or you could include actual dates and do something like this:
$currentTime = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:14:00");
$rangeStart = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:00:00");
$rangeEnd = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-02 07:55:00");
if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
echo 'in range';
} else {
echo 'not in range';
}
When start is after end you need to deal with a day change.
$current_time = "11:14 pm";
$start_time = "11:00 pm";
$end_time = "07:55 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time)->getTimestamp();
$date2 = DateTime::createFromFormat('H:i a', $start_time)->getTimestamp();;
$date3 = DateTime::createFromFormat('H:i a', $end_time)->getTimestamp();
if ($date3 < $date2) {
$date3 += 24 * 3600;
if ($date1 < $date2) {
$date1 += 24 *3600;
}
}
if ($date1 > $date2 && $date1 < $date3) {
echo 'in range';
} else {
echo 'not in range';
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if a date is in a given range?
How to check if date(entered by user) is in given range (Date format :-day month ie.:-1 june )
I am trying to find whether a date is in defined range. I'm using the following code:
$apple='25 March';
$udate= date('d F',strtotime($apple));
echo $udate;
$startDate='21 March';
$realStartDate= date('d F',strtotime($startDate)) ;
echo $realStartDate;
$endDate='19 April';
$realEndDate= date('d F',strtotime($endDate)) ;
if ($udate >= $realStartDate && $udate <= $realEndDate ) {
echo 'within tange';
}
else{
echo 'Not in range';
}
?>
Where am I going wrong?
try this one its working......
<?php
$udate = '25 March';
$udateTimestamp = strtotime($udate);
$startDate = '21 March';
$startDateTimestamp = strtotime($startDate);
$endDate = '19 April';
$eEndDateTimestamp = strtotime($endDate);
if ($udateTimestamp >= $startDateTimestamp && $udateTimestamp <= $eEndDateTimestamp)
{
echo 'within tange';
}
else
{
echo 'Not in range';
}
?>
Compare timestamps not the string representations!
if(strtotime($apple) < strtotime($endDate) && strtotime($apple) > strtotime($startDate)){
// All ok!
}
Like this
if(strtotime($givendate) > strtotime('3/21/xxxx') && strtotime($givendata) < strtotime('4/19/xxxx')) {
// Its within range
}
You can use DateTime
$userDate = new DateTime("2012-03-01");
if ( $userDate > new DateTime("2012-03-21 00:00:00") && $userDate < new DateTime("2012-04-19 23:59:59"))
{
// In Range
}
Putting it in a function if format is (1 july)
if (inRange ( "1 June", "3 March", "7 December" )) {
echo "In Range";
} else {
echo "Out Of Range";
}
function inRange($dateCheck, $dateFrom, $dateTo) {
$date = DateTime::createFromFormat ( "d F", $dateCheck );
$date1 = DateTime::createFromFormat ( "d F", $dateFrom );
$date2 = DateTime::createFromFormat ( "d F", $dateTo );
if ($date > $date1 && $date < $date2) {
return true;
}
return false;
}
try this
if (strtotime($udate) >= strtotime($realStartDate) && strtotime($udate) <= strtotime($realEndDate) ) {
echo 'within tange';
}
else{
echo 'Not in range';
}
I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>