【发布时间】:2012-01-22 09:51:24
【问题描述】:
是否可以使用 PHP 的 Relative Date Time format 获取一周的第一个/最后一个日期?
我已经尝试过:
date_default_timezone_set('Europe/Amsterdam');
$date = new DateTime();
$date->modify('first day of this week'); // to get the current week's first date
echo $date->format('Y-m-d'); // outputs 2011-12-19
$date->modify('first day of week 50'); // to get the first date of any week by weeknumber
echo $date->format('Y-m-d'); // outputs 2011-12-18
$date->modify('last day of this week'); // to get the current week's last date
echo $date->format('Y-m-d'); // outputs 2011-12-17
$date->modify('last day of week 50'); // to get the last date of any week by weeknumber
echo $date->format('Y-m-d'); // outputs 2011-12-18
你可以see 它不会输出正确的日期。
根据the docs,如果我是正确的,这应该是可能的。
我做错了什么吗?
编辑
我需要使用 PHP 的 DateTime 作为遥远未来的日期。
更新
现在变得更加陌生了。我又做了一些测试。
Windows PHP 5.3.3
2011-12-01
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (first day of week 50) at position 13 (w): The timezone could not be found in the database in C:\Users\Gerrie\Desktop\ph\Websites\Charts\www.charts.com\public\index.php on line 9
2011-12-01
2011-11-30
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (last day of week 50) at position 12 (w): The timezone could not be found in the database in C:\Users\Gerrie\Desktop\ph\Websites\Charts\www.charts.com\public\index.php on line 15
2011-11-30
Linux 5.3.8
2011-12-01
2011-12-01
2011-11-30
2011-11-30
【问题讨论】:
-
如果星期天或星期一是一周的第一天,这难道不取决于语言环境吗?
-
@mario:我不知道你告诉我。无论哪种方式,前两个应该输出相同的东西。最后两个恕我直言也是如此。是否从周日/周一开始