以下是我为把周日作为一周的开始日而创建的功能,
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;
}在这里,肝脏时间是一周中任何一天的划时代时间。基本上,这个函数取肝时间,并在上周日寻找,以便得到那个肝时阶段的当前间隔。
但是这里的问题是,每当我试图从这个得到当前的时间间隔时,
$createstart = strtotime('last Sunday', $getdate);给我
-345600。我不明白为什么?请大家分享一下这方面的信息。
这通常发生在过去的约会中,如
2007-10-02
发布于 2011-03-21 10:53:27
Strtotime的第二个参数是时间戳,而不是日期的字符串表示形式。尝试:
$createstart = strtotime('last Sunday', $liveratetime);它给出了-345600,因为当$getdate,即Y-m-d,被解析为int时,结果是0-时代时间。所以,上周日从划时代的时间开始.
发布于 2011-03-21 11:14:37
您可能需要尝试此函数,它将根据提供的日期从周日开始返回一个日期数组。
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;
}用法
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)发布于 2012-07-19 08:13:56
我在寻找一个解决这个问题的方法,经过一番研究和尝试,这似乎是可行的.虽然我仍然需要在下个星期天测试,看看它是否真的..。总之,这是代码:
$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 https://stackoverflow.com/questions/5376484
复制相似问题