以下是我创建的用于将星期日作为一周的开始日的函数,
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);
都会给我 -345600
。我不明白为什么?任何人都可以分享一下这个问题吗?
这通常发生在过去的日期,例如
2007-10-02
最佳答案
您可能想尝试这个函数,它将根据提供的日期返回一个日期数组,从星期日开始。
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)
关于php - 从周日开始获取一周的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5376484/