获取当周和过去8周的每个星期一的日期?

时间:2015-10-20 18:14:25

标签: php

我有一个下拉列表,我想显示星期一当前周和过去8周的日期。我该怎么做。以下是我正在使用的代码示例。

$maxDays=date('t');

for($i=0;$i<=$maxDays;$i++){
        echo  date("m-d-Y",strtotime("monday"));
        echo '<br>';
    }

2 个答案:

答案 0 :(得分:2)

您可能需要查看moment.php,PHP中的库来计算所有不同类型的日期/月/年。您的具体问题可归结为:

$m = new \Moment\Moment('2015-10-15T12:30:00', 'CET'); // last monday, that is
for($i=1;$i<=8;$i++){
    echo $m->subtractDays($i*7)->format("m-d-Y");  // $i multiplied by seven
    echo '<br>';
}

答案 1 :(得分:0)

你说:

  

我希望得到当月的每个星期一和过去8周的日期

所以,使用DateTimestrtotime

    $date = new \DateTime;
    $weekDay = 'Monday';
    $ts = strtotime('first day of next month');
    $date->setTimestamp($ts);
    $month = $date->format('m');
    $thisMonthTs = strtotime('first day of this month');
    $date->setTimestamp($thisMonthTs);
    $thisMonth = $date->format('m');

    while ($month >= $thisMonth) {
        $ts = strtotime("previous $weekDay", $ts);
        $date->setTimestamp($ts);
        $month = $date->format('m');
        echo $date->format('d-m-Y');
        echo '<br>';
    }

    for ($n = 0; $n < 7; $n++) {
        $ts = strtotime("previous $weekDay", $ts);
        $date->setTimestamp($ts);
        echo $date->format('d-m-Y');
        echo '<br>';
    }

解决方案可能会有所改进,因为这是一个快速的伎俩。但是你所需要的只是日期和时间

相关问题