Yii2 PHP获得第一天的一周和最后一天的日期

时间:2017-12-16 03:46:23

标签: yii2 yii2-basic-app

我有3个字段名称"date_submit""week_submit""month_submit"

如何在PHP中自动从日期字段获取周和月。例如:

如果"date_submit" = 2017-12-16"week_submit" = 2017-12-11"month_submit" = 2017-12-31

我正在使用Yii2基本模板。

感谢。

1 个答案:

答案 0 :(得分:1)

I am assuming you have $date_submit value and you want to get new desired values in $week_submit and $month_submit. Try this:

$date='2017-12-18';//$_POST['date_submit'];//date('Y-m-d');//'2017-12-19';
$day = DateTime::createFromFormat('Y-m-d', $date);
$day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1);
$week_submit = $day->format('Y-m-d');

$month_submit=date("Y-m-t", strtotime($date));

print_r ('Week submit: '.$week_submit.'<br>'.'Month submit: '.$month_submit);