如何查找给定日期所属的一周的第一天?

时间:2011-07-01 08:54:08

标签: php date strtotime

  

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

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

e.g。

$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,这表明我“一周的第一天”格式无效。

非常感谢任何想法,谢谢,

2 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分:-1)

测试它:

$time=[Your Request Time];

$dayofweek = date("w",strtotime($time));

if( $dayofweek != 0  )

    $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );

else

    $firstdayOfWeek = strtotime($time);