如何在PHP中获取本月的最后一天?
鉴于:
$a_date = "2009-11-23"
Run Code Online (Sandbox Code Playgroud)
我想要2009-11-30; 给定
$a_date = "2009-12-23"
Run Code Online (Sandbox Code Playgroud)
我想2009-12-31.
如何使用PHP查找一个月内的第一个和最后一个日期?例如,今天是2010年4月21日; 我想找到2010年4月1日和2010年4月30日.
我已经看过这个答案了,它与我所拥有的非常接近。
这是我的PHP代码:
$start = new DateTime('0:00 first day of previous month', new DateTimeZone('UTC'));
/*
if (isset($_GET['year']) && isset($_GET['month']) && checkdate($_GET['month'], 1, $_GET['year'])) {
$start = DateTime::createFromFormat('Y-m-d', $_GET['year'] . '-' . $_GET['month'] . '-1');
}*/
$middle = DateTime::createFromFormat('U', strtotime('first day of last month', $start->format('U')));
$middle->setTimezone(new DateTimeZone('UTC'));
$end = DateTime::createFromFormat('U', strtotime('first day of 2 months ago', $start->format('U')));
$end->setTimezone(new DateTimeZone('UTC'));
var_dump($start);
var_dump($middle);
var_dump($end);
Run Code Online (Sandbox Code Playgroud)
今天是 8 月 27 日,所以我预计是 7 月 1 日、6 月 1 日和 5 月 1 日。以下是实际输出:
object(DateTime)[1]
public …Run Code Online (Sandbox Code Playgroud)