php - 从周日开始获取一周的问题

标签 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);
    $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/

相关文章:

php - 无法使用 PHP 的 imagerotate 旋转图像

PHP 5.x strtotime 无法正确解析英国格式 d-m-y

php - 在php中指定时间后强制注销

php - 如何针对大数据更新和多个 API 调用优化 cron 和 PHP 脚本

php - 提交表单后出现错误时间 Php Mysql Strtotime

PHP 时区转换/PHP 日期未显示正确的时区

mysql - 将出生日期存储在数据库中作为 UNIX 时间?

php - 将 30 分钟添加到格式化为 H :i in PHP 的时间

javascript - 在 PHP 中调用自定义 JS 警报

javascript - json_encode 将空数组放在 json 末尾