3

I am using the following code to get a Y-m-d format of weekdays:

$monday = date('Y-m-d', strtotime('Monday'));
$tuesday = date('Y-m-d', strtotime('Tuesday'));
$wednesday = date('Y-m-d', strtotime('Wednesday'));
$thursday = date('Y-m-d', strtotime('Thursday'));
$friday = date('Y-m-d', strtotime('Friday'));

Today is 2012-08-01 (week 31) the Monday value I need should be 2012-07-30.

How come strtotime('Monday') makes it next monday?

4

3 回答 3

8

因为date('Y-m-d')返回今天的日期,即八月。你正在转换mondaytime. 现在时间是用date(Y-m-d)(2012 年 8 月)来表示的。所以显而易见的答案是从今天开始的下一个星期一。

所以要获得上周的日期,请使用

$monday=date(Y-m-d,strtotime('monday this week'))
于 2012-08-01T09:32:32.353 回答
1

它总是返回第二天的类型。下周一是 08-06,下周四是 08-02。

<?php
  function getDateOfWeekDay($day) {
    $weekDays = array(
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
    );

    $dayNumber = array_search($day, $weekDays);
    $currentDayNumber =  date('w', strtotime('today'));

    if ($dayNumber > $currentDayNumber) {
      return date('Y-m-d', strtotime($day));
    } else {
      return date('Y-m-d', strtotime($day) - 604800);
    }
  }

  echo  getDateOfWeekDay('Monday');
?>
于 2012-08-01T09:31:05.333 回答
1

对于我的应用程序,我只需要为本周的日期创建变量。

这就是我使用这段代码的原因:

$mon_value= date('Y-m-d', strtotime('Monday this week'));
$tue_value= date('Y-m-d', strtotime('Tuesday this week'));
$wed_value= date('Y-m-d', strtotime('Wednesday this week'));
$thu_value= date('Y-m-d', strtotime('Thursday this week'));
$fri_value= date('Y-m-d', strtotime('Friday this week'));
于 2012-08-01T09:44:15.620 回答