如何在php中获取周一和周日过去4周的日期?

时间:2016-06-17 06:48:17

标签: php date

我正在尝试使用PHP Monday生成Sundayeach week Date function的{​​{1}}日期。

我试过这样做。

<?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);

输出:

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
)

我得到的输出在本周和上周都是正确的,但在上周之前,一切都搞砸了,为什么会这样?

我甚至尝试过这样的方式。

<?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);

输出:

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在过去的4-8周内获得MondaySunday each week的日期。

使用:PHP版本5.3.3

更新:

我设法按照jeroen的建议获得了正确的日期,但是使用相同的日期功能,就像这样

<?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);

输出:

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
)

5 个答案:

答案 0 :(得分:2)

通过以7的倍数减少今天的方式尝试以下方法

    // 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>";

答案 1 :(得分:2)

使用时间戳和日期函数:

<?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);

输出:

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',
  ),
)

答案 2 :(得分:2)

你可以这个功能:

<%  grouped_options = [['North America',@north_america_names.collect {|v| [ v.name, v.id ] }],['Europe',@europe_names.collect {|v| [ v.name, v.id ] }]] %>

将返回抵消周的周一和周日。

demo

答案 3 :(得分:1)

不是用星期减去天数,而是使用当前日期的天数,如下所示。

 <?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'));
    ?>

答案 4 :(得分:0)

我制作了一份日期表,其日期已经细分,例如;

date - '2005-01-01', 
day - 'Saturday',
day-number -  1, 
month number -1, 
month - 'January', 
year number - 2005,
dt formated -  20050101
它从2005年1月1日到2049年。所以它使得你需要的东西超级简单。在这种情况下,我只会使用;

  

在CURDATE() - 30和之间选择dt FROM 000_00_Calender WHERE dt   CURDATE()和dt_day in(&#39; Monday&#39;,&#39; Sunday&#39;)

如果您愿意,可以获取文件here。它是一个.sql(1.12mb)

样品;

  

INSERT INTO 000_00_CalenderrecIDdtdt_dayday_number,   month_numbermonth_worddt_yeardate_formatedverified)   价值观(1,&#39; 2005-01-01&#39;,&#39;星期六&#39;,1,1,&#39; 1月&#39;,2005,20050101,   &#39; - &#39;),(2,&#39; 2005-01-02&#39;,&#39;星期日&#39;,2,1,&#39; 1月&#39;,2005 ,20050102,   &#39; - &#39;),(3,&#39; 2005-01-03&#39;,&#39;星期一&#39;,3,1,&#39; 1月&#39;,2005 ,20050103,   &#39; - &#39;),(4&#39; 2005-01-04&#39;,&#39;星期二&#39;,4,1,&#39; 1月&#39;,2005 ,20050104,   &#39; - &#39;),(5,&#39; 2005-01-05&#39;,&#39;星期三&#39;,5,1,&#39; 1月&#39;,2005 ,20050105,   &#39; - &#39;),...