Why does my IF statement doesn't catch the date in-between - php

I have many records in my database with different dates but my code doesn't catch the dates and it doesn't show up. What's the problem?
Here is my code
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s && $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s && $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}

Try this in your if statement as I mentioned earlier in the comment below your question, problem is with your if statement. so change that to this:
if (strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e))

Try changing your if statement like this
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s || $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s || $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}

I think you cannot use greater than or less than sign in a string
you should convert your variables as a strtotime.
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";

Because you need to convert dates to UNIX timestamp for comparison.
The below will return the desired result,
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}

Related

Calculating and adjusting dates based on exceptions (PHP)

Here are the rules:
The adding of days is always by 15s (ex. 15, 30, 45, 60, etc.)
When the maturity date day falls on the 15th or end of the month
(ex. 30 or 31 depends on the month, 28 or 29 every February depends
if leap year) when adding days (as mention above) the date
should fall ONLY to 15th or end of month.
When the maturity date day does not fall every 15th or end of the
month just normally add days.
When the date is February 14 and add 15 days it should return 02/29 if leap year or 02/28 if not leap year.
Here is my code but, I am getting 1 error and inconsistency.
Catchable fatal error when the date is 02/29/2020 and add 30 days.
What can I do to accommodate this rules?
function adjustDate($maturitydate, $add) {
$nodays = '+'.$add.' days';
$date = new DateTime($maturitydate);
$matdt = $date->modify($nodays);
$ismaturitydateendofmonth = check_end_of_month($maturitydate);
if($date->format('d') == 15) {
$matdt = $matdt->format('m/15/Y');
}
else if($ismaturitydateendofmonth == '1'){
$matdt->modify('last day of this month');
}
else{
$matdt = $matdt->format('m/d/Y');
}
return $matdt;
}
function check_end_of_month($date){
//adds 1 day to date
$Temp = date('m/d/Y',strtotime("+1 day", strtotime($date)));
//get the month of each date
$tempmonth = date('m', strtotime($Temp));
$datemonth = date('m', strtotime($date));
//check if the months are equal
if($tempmonth != $datemonth){
return '1';
}
else{
return '0';
}
}
The code below will fix all the exceptions and inconsistencies.
$matdt = $maturitydatefrom;
for ($x = 0; $x < $chknumdif; $x++) {
$matdt = getNextDuedate($matdt, $maturitydatefrom);
}
function getNextDuedate($prevdate, $maturitydate){
$maturityday = date('d', strtotime($maturitydate));
$prevday = date('d', strtotime($prevdate));
$prevmonth = date('m', strtotime($prevdate));
$prevyear = date('Y', strtotime($prevdate));
$prevlastday = date('t', strtotime($prevdate));
$maturitylastday = date('t', strtotime($maturitydate));
$isendofmonth = check_end_of_month($maturitydate);
if($maturityday == $maturitylastday || $maturityday == 15){
if($prevday == 15){
$duedate = $prevmonth . '/' . $prevlastday . '/' . $prevyear;
}
else{
$prevdate = date('m/d/Y', strtotime("+1 month", strtotime($prevdate)));
$duedate = date('m', strtotime($prevdate)) . '/15/' . date('Y', strtotime($prevdate));
}
}
else{
if($prevday < 15){
if($prevmonth == '2' && $prevday == '14'){
$duedate = $prevmonth . '/' . $prevlastday . '/' . $prevyear;
}
else{
$duedate = $prevmonth . '/' . ($prevday + 15) . '/' . $prevyear;
}
}
else if($prevday > 15){
if($maturityday < 15){
$prevdate = date('m/d/Y', strtotime("+1 month", strtotime($prevdate)));
$duedate = date('m', strtotime($prevdate)) . '/' . $maturityday . '/' . date('Y', strtotime($prevdate));
}
else{
$prevdate = date('m/d/Y', strtotime("+1 month", strtotime($prevdate)));
$duedate = date('m', strtotime($prevdate)) . '/' . ($maturityday - 15) . '/' . date('Y', strtotime($prevdate));
}
}
}
return $duedate;
}

Check the date between two dates

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

php time between 2 dates Y-m

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

Get Year From Date string

I want to check whether the current year is greater than a date string(D-M-Y) here is my code
$OldDate = "09-30-2011";
$OldYear = strtok($OldDate, '-');
$NewYear = date("Y");
if ($OldYear < $NewYear) {
echo "Year is less than current year"
} else {
echo "Year is greater than current year";
}
You can use strtotime():
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
UPDATE
Because you're using an unconventional datestamp, you have to use different methods, eg:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
NOTE: If you want to always be sure that your date gets correctly understood by strtotime, use YYYY-MM-DD
use the date function to get year
$OldDate = date("Y",strtotime("09-30-2011"));
$NewYear = date("Y",strtotime("now"));
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You can achieve your goal by doing:
$input_date = date("09-30-2011");
$input_date_arr = explode("-", $input_date);
$currYear = date("Y");
$inputYear = $input_date_arr[2];
if ($currYear > $inputYear) {
echo "Current year is greater than given year!";
} else {
echo "Current year is not greater than given year!";
}
$OldDate = "09-30-2011";
$OldYear = date('Y',strtotime($OldDate));
$NewYear = date("Y");
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You could convert the string to a timestamp, and check it with the current timestamp
if(time($OldDate) < time()){
// do stuff
} else {
// do other stuff
}
do this,
$dateString = '2021-02-24';
$yr = date("Y", strtotime($dateString));
$mon = date("m", strtotime($dateString));
$date = date("d", strtotime($dateString));

How to make a check if this timestamp is today,tomorrow or the day after tomorrow?

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

Categories