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


Get date of every monday of current week and the past 8 weeks?

我有一个下拉菜单,我想显示周一当前周和过去8周的日期。我怎么能做到呢?下面是我正在使用的代码示例。

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

您可能想看看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>';
}

你说:

我想获得当前月的每个星期一和过去8周的日期

使用DateTime和strtotime

    $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>';
    }

解决方案可以改进,因为它是一个快速的技巧。但是你只需要datetime和strtotime