日期问题
Issues with Dates
我需要获取每周周一至周日的范围,从当前开始。
这是我想出的:
$range = 0;
$weekNumber = date("W", strtotime(date('l j F Y') . ' ' . ($range) . ' days'));
$weekYear = date("Y", strtotime(date('l j F Y') . ' ' . ($range) . ' days'));
$week_array = getWeekDates($weekNumber, $weekYear);
function getWeekDates($week, $year) {
$dto = new DateTime();
$ret['mon'] = $dto->setISODate($year, $week)->format('Y-m-d');
$ret['tue'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['wed'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['thu'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['fri'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['sat'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['sun'] = $dto->modify('+1 days')->format('Y-m-d');
return $ret;
}
它在我的本地 WampServer (5.3.4) 上运行,但是当我在 Godaddy (5.2.17) 上尝试 运行 它时,出现此错误:
致命错误:在 /home/.....php 中的非对象上调用成员函数 format() on line ($ret['mon'] = $dto->setISODate($year, $week)->format('Y-m-d');)
根据 The Documentation, "setISODate" 和 "modify" 的 return 值从 5.3 开始从 NULL 更改为 DateTime。这就是为什么它适用于较新的 php 环境而不适用于较旧的环境。
为了使您的代码兼容,请将代码更改为如下内容:
$dto->setISODate($year, $week);
$ret['mon'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['tue'] = $dto->format('Y-m-d');
//so forth for rest of days
这里是函数的另一种写法:
function getWeekDates($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$keys = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
$ret = array();
for($i = 0; $i < 7; $i++) {
$ret[$keys[$i]] = $dto->format('Y-m-d');
$dto->modify('+1 days');
}
return $ret;
}
我需要获取每周周一至周日的范围,从当前开始。
这是我想出的:
$range = 0;
$weekNumber = date("W", strtotime(date('l j F Y') . ' ' . ($range) . ' days'));
$weekYear = date("Y", strtotime(date('l j F Y') . ' ' . ($range) . ' days'));
$week_array = getWeekDates($weekNumber, $weekYear);
function getWeekDates($week, $year) {
$dto = new DateTime();
$ret['mon'] = $dto->setISODate($year, $week)->format('Y-m-d');
$ret['tue'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['wed'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['thu'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['fri'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['sat'] = $dto->modify('+1 days')->format('Y-m-d');
$ret['sun'] = $dto->modify('+1 days')->format('Y-m-d');
return $ret;
}
它在我的本地 WampServer (5.3.4) 上运行,但是当我在 Godaddy (5.2.17) 上尝试 运行 它时,出现此错误:
致命错误:在 /home/.....php 中的非对象上调用成员函数 format() on line ($ret['mon'] = $dto->setISODate($year, $week)->format('Y-m-d');)
根据 The Documentation, "setISODate" 和 "modify" 的 return 值从 5.3 开始从 NULL 更改为 DateTime。这就是为什么它适用于较新的 php 环境而不适用于较旧的环境。
为了使您的代码兼容,请将代码更改为如下内容:
$dto->setISODate($year, $week);
$ret['mon'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['tue'] = $dto->format('Y-m-d');
//so forth for rest of days
这里是函数的另一种写法:
function getWeekDates($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$keys = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
$ret = array();
for($i = 0; $i < 7; $i++) {
$ret[$keys[$i]] = $dto->format('Y-m-d');
$dto->modify('+1 days');
}
return $ret;
}