Php 从周日开始的一周的问题

Php 从周日开始的一周的问题,php,strtotime,Php,Strtotime,以下是我创建的函数,用于将星期日作为一周的开始日 function getCurrentIntervalOfWeek($liveratetime) { // get start of each week. $dayofweek = date('w', $liveratetime); $getdate = date('Y-m-d', $liveratetime); $createstart = strtotime('last Sunday', $getdate);

以下是我创建的函数,用于将星期日作为一周的开始日


function getCurrentIntervalOfWeek($liveratetime) {
    // get start of each week.
    $dayofweek = date('w', $liveratetime);
    $getdate = date('Y-m-d', $liveratetime);
    $createstart = strtotime('last Sunday', $getdate);
    $weekstart = ($dayofweek == 0) ? $liveratetime : $createstart;
    // get the current time interval for a week, i.e. Sunday 00:00:00 UTC
    $currentInterval = mktime(0,0,0, date('m', $weekstart), date('d', $weekstart), date('Y', $weekstart));
    return $currentInterval;
}
这里liveratetime是一周中任何一天的划时代时间。基本上,此函数获取liveratetime并查找上个星期日,以便获取该liveratetime纪元的当前间隔

但这里的问题是,每当我试图从中获取当前时间间隔时,特定的liveratetime给了我。我不明白为什么?谁能跟我分享一下这件事吗

$createstart = strtotime('last Sunday', $getdate);

这通常发生在过去的日期,例如strotime的第二个参数是时间戳,而不是日期的字符串表示形式。
2007-10-02
尝试:


它给出了-345600,因为当$getdate(Y-m-d)被解析为int时,结果是0——纪元时间。因此,从大纪元时间算起的最后一个星期天是结果…

您可能想尝试此函数,它将根据提供的日期返回一个日期数组,从星期天开始

function get_week_dates( $date )
{
// the return array
$dates = array();

$time = strtotime($date);
$start = strtotime('last Sunday', $time);

$dates[] = date( 'Y-m-d', $start );

// calculate the rest of the times
for( $i = 1; $i < 7; $i++ )
{
    $dates[] = date( 'Y-m-d' , ( $start + ( $i * ( 60 * 60 * 24 ) ) ) );
}

return $dates;
}
会回来的

array
  0 => string '2011-03-20' (length=10)
  1 => string '2011-03-21' (length=10)
  2 => string '2011-03-22' (length=10)
  3 => string '2011-03-23' (length=10)
  4 => string '2011-03-24' (length=10)
  5 => string '2011-03-25' (length=10)
  6 => string '2011-03-26' (length=10)

我一直在寻找解决这个问题的方法,经过一些研究和尝试,似乎这是可行的。。。虽然我还需要在下周日进行测试,看看它是否真的有。。无论如何,代码如下:

$week_start = new DateTime();
$week = strftime("%U");  //this gets you the week number starting Sunday
$week_start->setISODate(2012,$week,0); //return the first day of the week with offset 0
echo $week_start -> format('d-M-Y'); //and just prints with formatting 

@srahul07正确阅读Bredis的答案。。。他说的是你对strotime的函数调用。
get_week_dates( '2011-03-21' );
array
  0 => string '2011-03-20' (length=10)
  1 => string '2011-03-21' (length=10)
  2 => string '2011-03-22' (length=10)
  3 => string '2011-03-23' (length=10)
  4 => string '2011-03-24' (length=10)
  5 => string '2011-03-25' (length=10)
  6 => string '2011-03-26' (length=10)
$week_start = new DateTime();
$week = strftime("%U");  //this gets you the week number starting Sunday
$week_start->setISODate(2012,$week,0); //return the first day of the week with offset 0
echo $week_start -> format('d-M-Y'); //and just prints with formatting