【问题标题】:How to find the first day of the week a given date belongs to? [duplicate]如何找到给定日期所属的一周的第一天? [复制]
【发布时间】:2011-09-26 14:30:12
【问题描述】:

可能重复:
Get first day of week in PHP?

给定一个时间戳,我需要找到该时间戳所属周的第一天。

例如

$format = "first day of week";

//convert to time
$time = strtotime("2011-07-01 00:00:00");

//format the time to the first day of the week
$date = strtotime($format,$time);

//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);

本周开始应该等于 2011-06-27,此时它发生了严重错误,等于 1970-01-01,这表明“一周的第一天”格式无效。

任何想法将不胜感激,谢谢,

【问题讨论】:

  • 你有没有 var_dumped 你的 strtotime 输出来看看你得到了什么?
  • 谢谢Pekka,答案是另一个问题的重复,是使用“上周日”的格式
  • $format = "一周的第一天";应该是“本周的第一天”,不是吗?

标签: php date strtotime


【解决方案1】:
$time = strtotime("2011-07-01 00:00:00");

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));

【讨论】:

    【解决方案2】:

    测试一下:

    $time=[Your Request Time];
    
    $dayofweek = date("w",strtotime($time));
    
    if( $dayofweek != 0  )
    
        $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );
    
    else
    
        $firstdayOfWeek = strtotime($time);
    

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多