简体   繁体   English

比较PHP中的日期和月份

[英]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. 第一个echo工作,我得到像12 Apr东西,基本上应该适合第二个if语句,但是,第二个echo不会打印任何东西,即使我在那些if语句中输入了echo "test ”之类的东西也没有被执行。 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 语法错误第三个strtotime得到了双倍((涵盖了第二个状态

   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. 首先,我不会用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. 我想你想要获得一年中的四分之一,但是这仅仅给出了januari的第1个结果和januari的15个结果,并且同样适用于4月,7月和10月。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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