如何使用PHP找到一个月的第一天和最后一天?例如,今天是2010年4月21日;我想要找到2010年4月1日和2010年4月30日。
最简单的方法是使用date
函数,它允许您混合硬编码的值和从时间戳中提取的值。如果不提供时间戳,则默认为当前日期和时间。
// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
$last_day_this_month = date('m-t-Y');
// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));
date()
搜索给定的字符串(如'm-t-Y'
),查找其中特定的符号并将它们替换为其时间戳中的值。因此,我们可以使用这些符号从时间戳中提取我们想要的值和格式。在上面的示例中:
Y
从时间戳中获取4位数的年份('2010')m
从时间戳中获取数字月份,并在前面加上零('04')t
从时间戳中获取该月份的天数('30')您可以根据需要进行创意操作。例如,要获取一个月的第一秒和最后一秒:
$timestamp = strtotime('February 2012');
$first_second = date('m-01-Y 00:00:00', $timestamp);
$last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
请参见http://php.net/manual/zh/function.date.php获取其他符号和更多详细信息。
简单的一句话
参考资料 - http://www.php.net/manual/en/function.date.php
<?php
echo 'First Date = ' . date('Y-m-01') . '<br />';
echo 'Last Date = ' . date('Y-m-t') . '<br />';
?>
// Get the timestamp for the date/month in question.
$ts = strtotime('April 2010');
echo date('t', $ts);
// Result: 30, therefore, April 30, 2010 is the last day of that month.
希望这有所帮助。
编辑:在阅读路易斯的回答之后,我想到您可能需要将日期格式化为正确的格式(YY-mm-dd)。这可能很明显,但并不妨碍提及。
// After the above code
echo date('Y-m-t', $ts);
这将给你本月的最后一天:
function lastday($month = '', $year = '') {
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
第一天:
function firstDay($month = '', $year = '')
{
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
return date('Y-m-d', $result);
}
对于特定的月份和年份,使用date()函数自然语言输出如下:
$first_date = date('d-m-Y',strtotime('first day of april 2010'));
$last_date = date('d-m-Y',strtotime('last day of april 2010'));
// Isn't it simple way?
但是针对当前月份。
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));
$date = '2012-02-12';//your given date
$first_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", first day of this month");
echo $first_date = date("Y-m-d",$first_date_find);
$last_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", last day of this month");
echo $last_date = date("Y-m-d",$last_date_find);
要获取当前日期,只需简单地使用以下代码:
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
获取上个月的第一天和最后一天:
$dateBegin = strtotime("first day of last month");
$dateEnd = strtotime("last day of last month");
echo date("D-F-Y", $dateBegin);
echo "<br>";
echo date("D-F-Y", $dateEnd);
简单格式
$curMonth = date('F');
$curYear = date('Y');
$timestamp = strtotime($curMonth.' '.$curYear);
$first_second = date('Y-m-01 00:00:00', $timestamp);
$last_second = date('Y-m-t 12:59:59', $timestamp);
下个月的更改将把 $curMonth 更改为 $curMonth = date('F',strtotime("+1 months"));
$startDate = date("Y-m-d",strtotime("Jan")); //2023-01-01
$endDate = date("Y-m-t",strtotime("Jan")); //2023-01-31
对于月份,您可以使用data('M')
作为strtotime()
函数的参数。
t
代表“给定月份的天数” - Pascal$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;
显示过去31天的日期