PHP check if today is in between two dates - php

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";
}
?>

Related

Do While in php for two different dates

Want to alert Effective Date, till it meets the condition.
Code:-
$effective_date = '2020/09/03';
$last_date = '2020/12/03';
$last_date = new DateTime($last_date);
$effective_date = new DateTime($effective_date);
do{
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
} while($effective_date > $last_date);
It alerts only one time, but my expectation is it should be alerted 3 times
It alerts only one time because your while condition is not correct.
Notice that the do { ... } while (condition) loop will run at least once and continue as long as the condition is true, not until it becomes true.
Change while($effective_date > $last_date) to while($effective_date < $last_date):
$effective_date = '2020/09/03';
$last_date = '2020/12/03';
$last_date = new DateTime($last_date);
$effective_date = new DateTime($effective_date);
do{
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
} while($effective_date < $last_date);
Ouput:
<script>alert('2020/09/03');</script><script>alert('2020/10/03');</script><script>alert('2020/11/03');</script>
Update
If you want the condition to be checked before the statements inside the loop are executed, use the while loop instead:
while ($effective_date < $last_date) {
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
}

How do I compare two datetime intervals in PHP?

I want to compare the interval of two datetimes to see if the interval is in the past, in the future or now.
$current_time = new DateTime();
$datetime1 = new DateTime('2018-03-17 18:25:00');
$datetime2 = new DateTime('2018-03-17 20:00:00');
if($current_time >= $datetime1 && $current_time <= $datetime2){
// now
} elseif($current_time >= $datetime1){
// past
} elseif($current_time <= $datetime1){
// future
}
EDIT:
Sorry, just realised posting my whole real code would make it easier for everyone.
The example above does work but it doesnt work when I loop thru the db using more than one interval from there
function interval(){
....
while($row = $result->fetch_assoc()){
$start_time = $row['start_time'];
$end_time = $row['end_time'];
$now = new DateTime();
$datetime1 = new DateTime($start_time);
$datetime2 = new DateTime($end_time);
if($now >= $datetime1 && $now <= $datetime2){
// now
}elseif($now < $datetime1 && $now < $datetime2){
// past
}elseif($now > $datetime1 && $now > $datetime2){
// future
}else{
// fail?
}
}
}
date returns a string. If you actually want to use a real DateTime object, then you need to do something like:
$now = new DateTime();
$other = new DateTime('2018-03-18 17:45:00'); //note: example made off the top of my head... value presented may not work
if ($now < $other) {
//do something
}
More information can, as always, be found in the PHP manual: https://secure.php.net/manual/en/class.datetime.php
As your dates are already string just compare them like that
$current_time = date("Y-m-d H:i:s");
$datetime1 = '2018-03-17 18:25:00';
$datetime2 = '2018-03-17 20:00:00';
if($current_time >= $datetime1 && $current_time <= $datetime2){
// now
} elseif($current_time >= $datetime1){
// past
} elseif($current_time <= $datetime1){
// future
}
Can you expand why your code isn't working as intended?
Below is how I would approach the question.
date_default_timezone_set('UTC');
$current_time = new DateTime();
$compareWith = new DateTime('2018-03-16 18:25:00');
if ($current_time >= $compareWith) {
//whatever your heart desires
}
else{
//whatever your heart desires
}

Year dynamically into date --- PHP

Does anyone know how to load current date data dynamically into a date in PHP ?
In example: the year, to automatically update.
I'm trying the following without success.
$nowDate = date('d/m/Y');
$cYear = date('Y');
$dateBegin = DateTime::createFromFormat('d/m/Y', '01/01/'.$cYear);
$dateEnd = DateTime::createFromFormat('d/m/Y', '31/12/'.$cYear);
if ($nowDate >= $dateBegin && $nowDate <= $dateEnd)
{
echo "is between";
} else {
echo 'OUT!';
}
You can compare DateTime objects between each-other:
$dateNow = new DateTime();
$dateBegin = new DateTime($dateNow->format('Y-01-01 00:00:00'));
$dateEnd = new DateTime($dateNow->format('Y-12-31 23:59:59'));
if ($dateBegin <= $dateNow && $dateNow <= $dateEnd) {
echo "is between";
} else {
echo 'OUT!';
}
demo
watch out data type
string date ( string $format [, int $timestamp ] )
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
make the same type and compare.
$nowDate = date('d/m/Y');
$cYear = date('Y');
$dateBegin = DateTime::createFromFormat('d/m/Y', '01/01/'.$cYear);
$dateEnd = DateTime::createFromFormat('d/m/Y', '31/12/'.$cYear);
// change to DateTime type
$nowDate2 = DateTime::createFromFormat('d/m/Y', $nowDate) ;
echo $nowDate2->format('Y-m-d')."\n";
echo $dateBegin->format('Y-m-d')."\n";
echo $dateEnd->format('Y-m-d')."\n";
if ($nowDate2 >= $dateBegin && $nowDate2 <= $dateEnd)
{
echo "is between";
} else {
echo 'OUT!';
}

check a date between two dates - 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

Function for selecting next Day If the last day is weekend

I am trying to create a function that checks the range of date. If the last date is an weekend it will select the next day. Suppose, Today is 1 March 2016. If someone add 5 with it, it will make the result 7 March 2016. Because 6 March is Weekend. I have written this code. But it is not working and I cannot find where is the problem. The code is as below:
<?php
$date = "2016-02-29";
$numOfDays = 8;
$futureDate = strtolower(date("l",strtotime($date." +".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate != $$w1 || $futureDate != $w2){
$finalDate = date("Y-m-d",strtotime($futureDate));
}
else{$finalDate = date("Y-m-d",strtotime($futureDate ."+1 day"));}
echo $finalDate;
?>
Check this:
<?php
$date = "2016-02-29";
$numOfDays = 11;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
echo $finalDate1;
?>
March 6th, 2016 is a Sunday, but you're checking for Friday and Saturday, you need to check for Saturday and Sunday instead, for the weekend. Also, your variable names need to match, and for Saturday you'll need to add two days to get the next weekday.
<?php
$date = "2016-02-29";
$numOfDays = 8;
$day = 86400;//one day is 86400 seconds
$time = strtotime($futureDate + $numOfDays * $day);//converts $futureDate to a UNIX timestamp
$futureDate = strtolower(date("l", $time));
$saturday = "saturday";
$sunday = "sunday";
if($futureDate == $saturday){
$finalDate = date("Y-m-d", $time + 2 * $day);
}
else if($futureDate == $sunday){
$finalDate = date("Y-m-d", $time + $day);
}
else{
$finalDate = date("Y-m-d", $time);
}
echo($finalDate);
?>
#Fakhruddin Ujjainwala
I have changed in your code and it works now perfect. Thanks. the code is now as below:
<?php
$date = "2016-02-29";
$numOfDays = 4;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
else if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
else{
$finalDate1 = date("Y-m-d",strtotime($futureDate1));
}
echo $finalDate1;
?>

Categories