【问题标题】:How to calculate the dates of particular days in the same week number from last year?如何计算去年同一周数中特定日期的日期?
【发布时间】:2012-05-25 09:44:03
【问题描述】:

我知道关于 SO 的大量日期问题,但我被这个问题难住了。

我需要生成一些报告,为此我需要计算出一些日期。

报告将在周一运行,我需要计算出前一周的周日和下周六的日期。

例如该报告将在 5 月 28 日星期一运行,我需要的日期是 5 月 20 日星期日和 5 月 26 日星期六。

然后我需要相同的值,但对于前一周,即 5 月 13 日星期日和 5 月 19 日星期六。

我认为这部分会很好,因为它只是获取运行报告时的星期一的当前日期并从那里进行操作的情况。

THEN 最后,我不能解决的部分是我提到的第一周,我需要去年的对应日期,所以周日和周六的日期来自同一周数在上一年。

我知道日期函数可以为您提供周数,但不知道如何根据此计算出该周的星期日和上一个星期六的日期。

这是我的,请原谅 OTT 变量名:

$today     = date('Y/m/d');

// reports run on a Monday and use last Sunday and the following Saturday's date so get yesterday's date first
$yesterday = strtotime ('-1 day', strtotime($today));
$yesterday = date ('Y/m/d', $yesterday);

// start of week (last Sunday)
$start_of_last_week       = strtotime ('-1 week', strtotime($yesterday));
$start_of_last_week       = date ('Y/m/d', $start_of_last_week);

// end of last week (the following Saturday)
$end_of_last_week       = strtotime ('+ 6 days', strtotime($start_of_last_week));
$end_of_last_week       = date ('Y/m/d', $end_of_last_week;

// start of previous week
$start_of_previous_week       = strtotime ('-1 week', strtotime($start_of_last_week));
$start_of_previous_week       = date ('Y/m/d', $start_of_previous_week);

// end of previous week
$end_of_previous_week       = strtotime ('+ 6 days', strtotime($start_of_previous_week));
$end_of_previous_week       = date ('Y/m/d', previous;



// the start of the same week last year
$start_of_last_week_last_year = strtotime ('-1 year', strtotime($start_of_last_week ));

但上面的不正确所以不知道下一步该怎么做。

非常感谢任何帮助。

【问题讨论】:

标签: php date


【解决方案1】:

您可以像这样找出特定年份和周数的星期一的日期

date( 'Y-m-d', strtotime( '2012W21' )); //2011-05-23

Compound Date Formats

【讨论】:

  • 哇,太棒了,在我认为之前从未听说过。有没有办法使用天数?例如周日的数字表示 (0)?如果不是,它仍然可以,因为星期一总是可以使用strtotime 来操纵。
  • 是的,date( 'Y-m-d', strtotime( '2012W210' )); 表示周日,注意周数后面的零,0 - 6 表示该周的周日到周六
猜你喜欢
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多