如何设置每周的日期范围值,以便随着时间的推移动态更新。我想在周一开始一周,在周日结束。
示例输出需要2011-10-24,2011-10-31
使用下面我只得到月份的日期与YYYY-MM-DD
<?php
$today = getdate();
$weekStartDate = $today['mday'] - $today['mon']+1;
$weekEndDate = $today['mday'] - $today['wday']+7;
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;
?>
答案 0 :(得分:2)
如评论中所述,请使用DateTime
。
<?php
$dt = new DateTime('Monday this week'); // yes, DateTime is that awesome
$interval = new DateInterval('P6D'); // 6 days
?>
<dl>
<dt>Week start date:</dt>
<dd><?php echo $dt->format('Y-m-d') ?></dd>
<dt>Week end date:</dt>
<dd><?php echo $dt->add($interval)->format('Y-m-d') ?></dd>
</dl>
答案 1 :(得分:0)
利用date('N')
(星期几,1 - 7):
$monday = mktime(0, 0, 0, date('n'), date('j') - (date('N') - 1));
$sunday = mktime(0, 0, 0, date('n'), date('j') - (date('N') - 7));
echo date('Y-m-d', $monday);
echo date('Y-m-d', $sunday);