简体   繁体   中英

Comparing date and month in PHP

I have the following code that gets a date from a timestamp in the DB and then I compare it with specific dates and accordignly set a variable to some value:

    $leavedate=$this->leavedate;
    $leavestamp=strtotime($leavedate[0]['LoppumisPvm']);
    $leave=date('d M',$leavestamp);
    echo "the leave is: ".$leave; //this one prints: 12 Apr

        $leavequarter="";
       if(strtotime($leave)>= strtotime('01 Jan') && strtotime(($leave)<= strtotime('15 Jan'))){$leavequarter=1;}
       elseif(strtotime($leave)>= strtotime('01 Apr') && strtotime(($leave)<= strtotime('15 Apr'))){$leavequarter=2;}
       elseif(strtotime($leave)>= strtotime('01 Jul') && strtotime(($leave)<= strtotime('15 Jul'))){$leavequarter=3;}
       elseif(strtotime($leave)>= strtotime('01 Oct') && strtotime(($leave)<= strtotime('15 Oct'))){$leavequarter=4;}


    echo $leavequarter;

The first echo works and I get something like 12 Apr which basically should fit in the second if statement, however, it the 2nd echo does not print anything and even if I put something like echo "test " in those if statements none gets executed. Am i doing something wrong?

You are nesting your code wrongly:

elseif(strtotime($leave)>= strtotime('01 Apr') && strtotime(($leave)<= strtotime('15 Apr')))

It should be:

elseif(strtotime($leave)>= strtotime('01 Apr') && strtotime($leave)<= strtotime('15 Apr'))
                                                           ^                             ^

Syntax mistake the third strtotime got double (( covering the how second state

   if(strtotime($leave)>= strtotime('01 Jan') && strtotime($leave)<= strtotime('15 Jan')){$leavequarter=1;}
   elseif(strtotime($leave)>= strtotime('01 Apr') && strtotime($leave)<= strtotime('15 Apr')){$leavequarter=2;}
   elseif(strtotime($leave)>= strtotime('01 Jul') && strtotime($leave)<= strtotime('15 Jul')){$leavequarter=3;}
   elseif(strtotime($leave)>= strtotime('01 Oct') && strtotime($leave)<= strtotime('15 Oct')){$leavequarter=4;}

first of all, i would not do it all with strtotime. this is resource consuming and depends on locales.

better to use the date function.

$leavedate = $this->leavedate;
$leavestamp = strtotime($leavedate[0]['LoppumisPvm']);
$leave = date('d M', $leavestamp);
echo "the leave is: " . $leave; //this one prints: 12 Apr

$leavequarter = "";
if (date('m', $leavestamp) == 1 && date('d', $leavestamp) >= 1 && date('d', $leavestamp) <= 15)
{
    $leavequarter = 1;
}
elseif (date('m', $leavestamp) == 4 && date('d', $leavestamp) >= 1 && date('d', $leavestamp) <= 15)
{
    $leavequarter = 2;
}
elseif (date('m', $leavestamp) == 7 && date('d', $leavestamp) >= 1 && date('d', $leavestamp) <= 15)
{
    $leavequarter = 3;
}
elseif (date('m', $leavestamp) == 10 && date('d', $leavestamp) >= 1 && date('d', $leavestamp) <= 15)
{
    $leavequarter = 4;
}


echo $leavequarter;

but what are you trying to do? i think you want to get the quarter of the year, but this only gives results for 1st januari to 15th of januari and same for april, july and october.

honestly i think this is what you want:

$leavedate = $this->leavedate;
$leavestamp = strtotime($leavedate[0]['LoppumisPvm']);
$leave = date('d M', $leavestamp);
echo "the leave is: " . $leave; //this one prints: 12 Apr

$leavequarter = (int) ((date('m', $leavestamp) - 1) / 3) + 1;
echo $leavequarter;

try this code:

if((strtotime($leave)>= strtotime('01 Jan')) && (strtotime($leave)<= strtotime('15 Jan'))){$leavequarter=1;}
    elseif((strtotime($leave)>= strtotime('01 Apr')) && (strtotime($leave)<= strtotime('15 Apr'))){$leavequarter=2;}
    elseif((strtotime($leave)>= strtotime('01 Jul')) && (strtotime($leave)<= strtotime('15 Jul'))){$leavequarter=3;}
    elseif((strtotime($leave)>= strtotime('01 Oct')) && (strtotime($leave)<= strtotime('15 Oct'))){$leavequarter=4;}   
echo $leavequarter;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM