我正试图获得星期一和星期日(哥斯达黎加分别为一周的第一天和最后一天)。所以,我这样做了:
$now = new \DateTime();
$monday = new \DateTime();
$monday = $monday->modify("monday this week");
$sunday = new \DateTime();
$sunday = $sunday->modify("sunday this week");
echo "timezone: " . date_default_timezone_get();
echo "<br>";
echo "now: " . $now->format('d/m/Y H:i');
echo "<br>";
echo "monday: " . $monday->format('d/m/Y H:i');
echo "<br>";
echo "sunday: " . $sunday->format('d/m/Y H:i');
输出结果为:
timezone: America/Costa_Rica
now: 26/10/2014 19:01
monday: 27/10/2014 00:00
sunday: 02/11/2014 00:00
但是,当前周有可能指向未来的日期?
答案 0 :(得分:1)
试试这个:
$monday = new DateTime();
// create DateTime object with current time
$monday->setISODate($monday->format('o'), $monday->format('W'));
// set object to Monday this week
$sunday = clone $monday;
// clone DateTime object
$sunday->modify('+6 day');
// add 6 days to Monday, to get the Sunday
echo sprintf(
'This week is stared on %s, and ended on %s.',
$monday->format('Y-m-d'),
$sunday->format('Y-m-d')
);
// just print out the result