【问题标题】:Get first and third monday of each month - strtotime获取每个月的第一个和第三个星期一 - strtotime
【发布时间】:2013-06-04 10:19:03
【问题描述】:

我想在网站上自动显示每个月的第一个和第三个星期一。如果当前日期在第一个星期一之后,则只需要显示第三个星期一。

我修改了在论坛上找到的代码并进行了一些更改,但由于 php 知识有限,我无法验证它是否会提供正确的结果。

$time = strtotime("first Monday of ".$monthname." ".$year); 
$time2 = strtotime("third Monday of ".$monthname." ".$year); 
{
    if ($time2 > $time) {
        echo date('d-m-Y',$time2)." ".$monthname;
    }
    else {
        echo date('d-m-Y',$time)." ".$monthname;
    }
}  

【问题讨论】:

    标签: date time calendar strtotime


    【解决方案1】:

    我不完全确定你的意思,但这应该符合我的想法:

    $time1 = strtotime("first Monday of {$monthname} {$year}"); 
    $time2 = strtotime("third Monday of {$monthname} {$year}");
    echo date('jS F Y', time() > $time1 ? $time2 : $time1); // e.g. 1st January 1970
    

    time() > $time1 ? $time2 : $time1 是一个三元条件,基本上意味着

    condition ? if_true : if_false
    

    按照你写的方式,我想你需要知道你可以将变量放在双引号内,例如

    $a = 'first';
    echo "The $a day of the week"; // echoes 'The first day of the week
    

    但不是单引号,例如

    $a = 'first';
    echo 'The $a day of the week'; // echoes 'The $a day of the week.
    

    我在变量周围加上了大括号,这样我就可以做到了

    $a = 'first';
    $b = 'variable';
    echo "This is the {$a}_{$b}"; // Echoes 'This is the first_variable'
    

    没有大括号

    echo "This is the $a_$b" // Undefined variable $a_
    

    try {
        // Do something
    } catch (Exception $ex) {
        echo "There was an error and the message was {$ex->getMessage()}";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2014-07-21
      • 1970-01-01
      相关资源
      最近更新 更多