首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中获得最后4周的日期(周一和周日)?

如何在php中获得最后4周的日期(周一和周日)?
EN

Stack Overflow用户
提问于 2016-06-17 06:48:17
回答 4查看 3.6K关注 0票数 1

我试图使用PHP的MondaySunday of each week生成最后4-8周的日期。

我试过这样做。

代码语言:javascript
复制
<?php
$dates_array = array();

$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Last Monday - 3 week"));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Last Sunday - 3 week"));

$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Last Monday - 2 week"));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Last Sunday - 2 week"));

$dates_array['last monday'] = date('Y-m-d',strtotime("Last Monday"));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Last Sunday"));

$dates_array['this monday'] = date('Y-m-d',strtotime("Monday"));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday"));

print_r($dates_array);

产出:

代码语言:javascript
复制
Array
(
    [-3 week monday] => 2016-05-16
    [-4 week sunday] => 2016-05-15
    [-2 week monday] => 2016-05-30
    [-2 week sunday] => 2016-05-29
    [-1 week monday] => 2016-06-06
    [-1 week sunday] => 2016-06-05
    [last monday] => 2016-06-06
    [last sunday] => 2016-06-12
    [this monday] => 2016-06-13
    [this sunday] => 2016-06-19
)

我得到的产出对于本周和上周都是正确的,但在上周之前,一切都是一团糟,这是为什么?

我甚至试过这样做。

代码语言:javascript
复制
<?php
$dates_array = array();

$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('-3 week')));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-3 week')));

$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('-2 week')));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-2 week')));

$dates_array['last monday'] = date('Y-m-d',strtotime("Monday", strtotime('-1 week')));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-1 week')));

$dates_array['this monday'] = date('Y-m-d',strtotime("Monday"));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday"));

print_r($dates_array);

产出:

代码语言:javascript
复制
Array
(
    [-3 week monday] => 2016-05-30
    [-3 week sunday] => 2016-05-29
    [-2 week monday] => 2016-06-06
    [-2 week sunday] => 2016-06-05
    [last monday] => 2016-06-13
    [last sunday] => 2016-06-12
    [this monday] => 2016-06-20
    [this sunday] => 2016-06-19
)

在上面的例子中,输出日期也很奇怪。

那么,使用PHP获取MondaySunday of each week最后4-8周日期的最佳方法是什么?

使用: PHP版本5.3.3

最新情况:

我设法得到了jeroen建议的正确日期,但是使用了相同的日期函数,如下所示

代码语言:javascript
复制
<?php

$dates_array = array();

$dates_array['-4 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -4 week')));
$dates_array['-4 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -4 week')));

$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -3 week')));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -3 week')));

$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -2 week')));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -2 week')));

$dates_array['last monday'] = date('Y-m-d',strtotime("Monday", strtotime('last week')));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('last week')));

$dates_array['this monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week')));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week')));

print_r($dates_array);

产出:

代码语言:javascript
复制
Array
(
    [-4 week monday] => 2016-05-16
    [-4 week sunday] => 2016-05-22
    [-3 week monday] => 2016-05-23
    [-3 week sunday] => 2016-05-29
    [-2 week monday] => 2016-05-30
    [-2 week sunday] => 2016-06-05
    [last monday] => 2016-06-06
    [last sunday] => 2016-06-12
    [this monday] => 2016-06-13
    [this sunday] => 2016-06-19
)
EN

回答 4

Stack Overflow用户

发布于 2016-06-17 07:21:48

使用时间戳和日期函数:

代码语言:javascript
复制
<?php
$timestamp = strtotime('20160604');

$day_of_the_week = date('N', $timestamp); // N returns mon-sun as digits 1 - 7. 

$sunday_ts = $timestamp + ( 7 - $day_of_the_week) * 24 * 60 * 60;
$monday_ts = $timestamp - ( $day_of_the_week - 1) * 24 * 60 * 60;

$dates = array();
for($i = 1; $i < 5; $i++) {
    $dates_key = '-' . $i . ' Week';
    $dates[$dates_key] = array(
        'Monday' => date('Y m d', $monday_ts - $i * 7 * 24 * 60 * 60),
        'Sunday' => date('Y m d', $sunday_ts - $i * 7 * 24 * 60 * 60)
        );
}

printf("Given date %s falls on a %s.\n", date('Y m d', $timestamp), date('l', $timestamp));
var_export($dates);

输出:

代码语言:javascript
复制
Given date 2016 06 04 falls on a Saturday.
array (
  '-1 Week' => 
  array (
    'Monday' => '2016 05 23',
    'Sunday' => '2016 05 29',
  ),
  '-2 Week' => 
  array (
    'Monday' => '2016 05 16',
    'Sunday' => '2016 05 22',
  ),
  '-3 Week' => 
  array (
    'Monday' => '2016 05 09',
    'Sunday' => '2016 05 15',
  ),
  '-4 Week' => 
  array (
    'Monday' => '2016 05 02',
    'Sunday' => '2016 05 08',
  ),
)
票数 3
EN

Stack Overflow用户

发布于 2016-06-17 07:14:38

尝试以下方法,减少今天的倍数为7

代码语言:javascript
复制
    // This Week
    echo date("d-m-Y", strtotime("this Monday"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday"))."<br>";

    // Last Week
    echo date("d-m-Y", strtotime("this Monday -7 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -7 day"))."<br>";

    // -2 Week
    echo date("d-m-Y", strtotime("this Monday -14 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -14 day"))."<br>";

    // -3 Week
    echo date("d-m-Y", strtotime("this Monday -21 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -21 day"))."<br>";

    // -4 Week
    echo date("d-m-Y", strtotime("this Monday -28 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -28 day"))."<br>";

    // -5 Week
    echo date("d-m-Y", strtotime("this Monday -35 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -35 day"))."<br>";

    // -6 Week
    echo date("d-m-Y", strtotime("this Monday -42 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -42 day"))."<br>";

    // -7 Week
    echo date("d-m-Y", strtotime("this Monday -49 day"))."<br>";
    echo date("d-m-Y", strtotime("this Sunday -49 day"))."<br>";
票数 2
EN

Stack Overflow用户

发布于 2016-06-17 07:07:16

与其用周减去天数,不如使用当前日期的天数,如下所示。

代码语言:javascript
复制
 <?php
    date('Y-m-d',strtotime("Monday"));
    echo date('Y-m-d',strtotime('last monday  days'));
    echo date('Y-m-d',strtotime('last monday -7 days'));
    echo date('Y-m-d',strtotime('last monday -14 days'));
    echo date('Y-m-d',strtotime('last monday -21 days'));
    echo date('Y-m-d',strtotime('last monday -28 days'));
    ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37874894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档