简体   繁体   English

PHP开关/案例语句中的strtotime时间/日期

[英]strtotime time/date in PHP switch/case statement

I've created a PHP switch / case statement: 我创建了一个PHP switch / case语句:

switch(true)
{
    case ($eff >= '10/2017'):
        echo "Greater than 10-2017";
        break;
    case ($eff <= '09/2017'):
        echo "Less thank 10-2017";
        break;                          
    default:
        echo '';
        break;
}

I'm using strtotime to cut down a date from 08/01/2017 to just 08/2017 like this: 我正在使用strtotime将日期从08/01/2017 08/201708/2017如下所示:

$eff = date("m/Y", strtotime($userinfo['ExpDate']));

Unfortunately when I use this to create my case statement it doesn't give me the conditional output. 不幸的是,当我使用它创建我的case语句时,它没有提供条件输出。 I was hoping for when using a > or < operator and just defaults to the first case statement. 我希望使用><运算符,并且默认使用第一个case语句。

How do I use this to properly use greater/less than? 如何使用它正确使用大于/小于?

You can use a solution like the following: 您可以使用以下解决方案:

<?php
$eff = date("m/Y", strtotime('10/01/2017'));
$eff = date_create_from_format('m/Y', $eff);

switch(true)
{
    case (date_create_from_format('m/Y', '10/2017')->diff($eff)->format('%R%m') >= 0):
        echo "Greater than 10-2017";
        break;
    case (date_create_from_format('m/Y', '09/2017')->diff($eff)->format('%R%m') <= 0):
        echo "Less thank 10-2017";
        break;                          
    default:
        echo '';
        break;
}

demo: https://ideone.com/Z2un3C / some tests: https://3v4l.org/YP1Ub 演示: https : //ideone.com/Z2un3C / 一些测试: https : //3v4l.org/YP1Ub

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

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