动态更改事件之间的日期

时间:2016-06-24 05:13:40

标签: php

基于来自Rounin的一段代码 - 我已经采取了这个尝试并提出了一种方法,基本上如果今天的日期介于2017-03-01和2017-04-30之间,那么显示rego表格。如果超出这些日期,则显示"表格不可用"

中间的日期是两个设定的日期很容易理解,但我想要做的是动态更改Y,所以我不必每年手动编辑。

我拥有的是什么(虽然看起来非常笨重但是有效?)

<?php

        $today = date('Y-m-d');


        $Current_Year = date('y');
        $Current_Month = date('n');
        $Current_Day = date('j');
        $Next_Year = ($Current_Year + 1);

        if ($Current_Month < 5) {
        $Begin_Rego_Year = $Current_Year;
        }

        if ($Current_Month > 3) {
        $Begin_Rego_Year = $Next_Year;
        }

        # Checks if date is March 31st
        if (($Current_Month == 3) && ($Current_Day == 1)) {
        $Begin_Rego_Year = $Current_Year;
        }

        $Begin_Rego_Date = $Begin_Rego_Year.'-03-01';


        /////////////////

        if ($Current_Month < 5) {
        $End_Rego_Year = $Current_Year;
        }

        if ($Current_Month > 3) {
        $End_Rego_Year = $Next_Year;
        }

        # Checks if date is March 31st
        if (($Current_Month == 4) && ($Current_Day == 30)) {
        $End_Rego_Year = $Current_Year;
        }

        $End_Rego_Date = $End_Rego_Year.'-04-30';

        if (($today >= $Begin_Rego_Date) && ($today <= $End_Rego_Date))
{
   echo "Display Rego Form"; //if todays date was between 01 Mar - 30 Apr 17
}

else {

   echo "Rego Form Offline until 01 Mar 18";
}

?>

2 个答案:

答案 0 :(得分:0)

使用日期的简单方法我认为它是一个函数strtotime
@usage示例 strtotime();

if (
   strtotime('today')  >= strtotime('first day of this month') 
   && strtotime('today') <= strtotime('last day of this month')
) { 
    echo 'display rego form'; 
}

php.net
Info about this function
Info about date formats for strtotime

答案 1 :(得分:0)

不确定这是否合法.. strtotime('四月的最后一天') - 无法使其正常工作。

结束(到目前为止)

$open_day = date('01-03-Y');
$close_day = date('30-04-Y');

if (
   strtotime('today')  >= strtotime($open_day) && strtotime('today') <= strtotime($close_day)
) { 
    echo 'display rego form'; 
}
else {
    echo 'Come back another time';
}