check a date between two dates - PHP - php

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

Related

Avoid loop for Date add by day?

I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));

How to find out the number of days between two dates

i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
its not working, not giving the number of days between given two dates.
Try this. It is very simple.
<?php
$date1 = strtotime("2015-11-16 10:01:13");
$date2 = strtotime("2015-05-06 09:47:16");
$datediff = $date1 - $date2;
echo floor($datediff/(60*60*24))." days"; //output 194 days
?>
It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question
Try this,
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$datediff = $pur_dt - $todate;
$diff = $datediff/(60*60*24);
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try This :
$pur_dt = Date('2015-08-03');
$todate = Date(date('Y-m-d'));
$pur_dt = strtotime($pur_dt);
$todate = strtotime($todate);
$seconds_diff = $todate - $pur_dt;
$$diff = floor($seconds_diff/(60*60*24));
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try this
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff->days>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}

Error during third saturday date fetching

I want to fetch third Saturday and I am using php function for that, that i know.
But I am getting wrong data while fetching from an error.
Here is my code:
$frmdate = 2015-06-05;
$todate = 2015-08-31;
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
Where am I wrong?
Every Months contain only one "third saturday" , so no need to do more looping of days. Just try this Code Once.
$frmdate = "2015-06-05";
$todate = "2015-08-31";
$custom_third_sat=array();
for ($date = date("Y-m-01", strtotime($frmdate)); $date <= $todate; $date = date("Y-m-01",strtotime($date."+1 Month"))) {
if($date>$todate){
break;
}
$t_date=date('Y-m-d', strtotime($date.' third Saturday'));
if($t_date>=$frmdate && $t_date<=$todate)
{
$custom_third_sat[] = $t_date;
}
}
echo "<pre>";print_r($custom_third_sat);
you should use of like third saturday of:try this
$custom_third_sat[] = date('Y-m-d', strtotime("third saturday of $custom_day"));
your full code can be something like this:
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
if(!isset($custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))])){
$custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))] = date('Y-m-d', strtotime("third saturday of $custom_day"));
}
}
echo "<pre>";
print_r($custom_third_sat);
You are just missing the quotes to dates
<?php
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
echo"assa";
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
?>

PHP check if today is in between two dates

Consider following code:
$today = date("d/m/Y");
$start_date = '20/11/2014';
$time1 = strtotime($start_date1);
$start_date1 = date('d/m/Y',$time1);
$end_date = '20/01/2015';
$time2 = strtotime($end_date1);
$end_date1 = date('d/m/Y',$time2);
if( $start_date1 <= $today && $end_date1 >= $today)
echo "yes";
else
echo 'no';
Even though $today is in between those start and end date, I get "no" in return. What might be the problem here? I just wanna check if today is in between those dates. Start date and end date are saved as string in DB.
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";
}
?>

Last day of each month within a date range in php

How to get last day of each month within a date range in PHP?
Input:
$startdate = '2013-01-15'
$enddate = '2013-03-15'
The output that I want is:
2013-01-31 End date of January
2013-02-28 End date of February
2013-03-15 *End date of March is '2013-03-31' but the end date is '2013-03-15' .
So I want 2013-03-15.
How can I do that?
In the future, try to write the code yourself. If you are stuck with a specific part you can request help. Exception for this one time:
<?php
$startdate = new DateTime('2013-01-15');
$enddate = new DateTime('2013-04-15');
$year = $startdate->format('Y');
$start_month = (int)$startdate->format('m');
$end_month = (int)$enddate->format('m');
for ( $i=$start_month; $i<$end_month; $i++) {
$date = new DateTime($year.'-'.$i);
echo $date->format('Y-m-t').' End date of '.$date->format('F');
}
echo $enddate->format('Y-m-d');
Will output:
2013-01-31 End date of January
2013-02-28 End date of February
2013-03-31 End date of March
2013-04-15
Note that this does not work if the start and end dates have a different year. I have left this as an exercise.
function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date('mY', $time2);
$months = array(date('Y-m-t', $time1));
$f = '';
while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));
if(date('F', $time1) != $f) {
$f = date('F', $time1);
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = date('Y-m-t', $time1);
}
}
$months[] = date('Y-m-d', $time2);
return $months;
}
$myDates = get_months('2005-01-20', '2005-11-25');
$myDates will have the output you want. It will work even if years are different. Logic is from this URL
I'm little changed the function:
function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date('mY', $time2);
$f = '';
while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));
if(date('F', $time1) != $f) {
$f = date('F', $time1);
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = array(date('Y-m-01', $time1),date('Y-m-t', $time1));
}
}
$months[] = array(date('Y-m-01', $time2),date('Y-m-d', $time2));
return $months;
}

Categories