简体   繁体   English

获取PHP的第一天/最后一天?

[英]Get first/last day of week in php?

With get the date of the first day of the week and last day of the week in php, first day Monday and the last day Friday, for example I have the date 2017-05-23 and I want to know the Monday one 2017-05-22 and the last one that would be 2017-05-26 在php,第一天星期一和星期五的最后一天获取星期一和星期的最后一天的日期,例如我有2017-23的日期,我想知道2017年的星期一 - 05-22和最后一个将是2017-05-26

date now = '2017-05-23'
first day of week = '2017-05-22' (Monday)
last day of week = '2017-05-26' (Friday)

Can I do this using date ? 我可以使用date这样做吗?

Something like this: 像这样的东西:

$mon = $fri = new DateTime('2017-05-23');
$mon->modify('Last Monday');
$fri->modify('Next Friday');
var_dump($mon);
var_dump($fri);
$the_date = '2017-05-23';
$the_day_of_week = date("w",strtotime($the_date)); //sunday is 0

$first_day_of_week = date("Y-m-d",strtotime( $the_date )-60*60*24*($the_day_of_week)+60*60*24*1 );
$last_day_of_week = date("Y-m-d",strtotime($first_day_of_week)+60*60*24*4 );

echo $first_day_of_week;
echo "~";
echo $last_day_of_week;

there is a solution but I'm not sure if you will accept it like this 有一个解决方案,但我不确定你是否会接受这样的

$First_date = date_create('this week')->format('Y-m-d H:i:s');
$Last_date = date_create('this week +4 days')->format('Y-m-d H:i:s');
echo $First_date;
echo $Last_date;

My solve, 我的解决,

$datenow = date('Y-m-d');// date now
$mon = new DateTime($datenow);
$fri = new DateTime($datenow);
$mon->modify('Last Monday');
$fri->modify('Next Friday');

using echo 使用echo

echo 'Monday: '.$mon->format('Y-m-d').'<br>';
echo 'Friday: '.$fri->format('Y-m-d').'<br>';

using var_dump() 使用var_dump()

var_dump($mon);
var_dump($fri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM