如何获得Date of Monday
和Date of Sunday
如果我有mysql
DATETIME
格式的具体日期?PHP如何获得给定日期下周的星期一和星期日的日期?
我想要得到本周的第一天的日期和给定日期下降的最后一天的日期。
我有日期'2016-06-05'它在'Y-m-d'
我想这样。
<?php
$date = '2016-06-05';
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
?>
但它给人
2016-06-06
2016-06-12
这是不对的一周,它应该给
2016-05-30
2016-06-05
我甚至试过这样的方式。
$date = '2016-06-05';
echo date("Y-m-d", strtotime('monday', strtotime('this week', strtotime($date)))), "\n";
echo date("Y-m-d", strtotime('sunday', strtotime('this week', strtotime($date)))), "\n";
OR
$date = '2016-06-05';
echo date("Y-m-d", strtotime('monday', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday', strtotime($date))), "\n";
使用PHP 5.3
缺少什么我在这里?
UPDATE:
我想出了这一点,这是给予预期的输出。
function get_monday_sunday($date){
$dates_array = array();
if(date('N', strtotime($date)) == 7){
$dates_array['monday'] = date("Y-m-d", strtotime('Monday last week', strtotime($date)));
$dates_array['sunday'] = date("Y-m-d", strtotime('Sunday last week', strtotime($date)));
}else{
$dates_array['monday'] = date("Y-m-d", strtotime('Monday this week', strtotime($date)));
$dates_array['sunday'] = date("Y-m-d", strtotime('Sunday this week', strtotime($date)));
}
return $dates_array;
}
$date = '2016-06-05'
print_r(get_monday_sunday($date));
看起来像当一天是星期的最后一天,然后下周重新开始,即php周开始是星期日我猜。
echo date(“Y-m-d”,strtotime('上周日',strtotime($ date))),“\ n”;因为星期日是一周中的最后一天,在php – splash58
谢谢,我有很多日期,它会在任何给定的日期工作吗? – AMB
我不确定周日是第一天还是最后一天不是由服务器配置设置的。但在目前的环境下,它将起作用 – splash58