如何使用PHP查找一个月内的第一个和最后一个日期?
如何使用PHP查找一个月内的第一个和最后一个日期?例如,今天是2010年4月21日;我要查找2010年4月1日和2010年4月30日。这将为您提供一个月的最后一天:如何使用PHP查找一个月内的第一个和最后一个日期?,php,date,Php,Date,如何使用PHP查找一个月内的第一个和最后一个日期?例如,今天是2010年4月21日;我要查找2010年4月1日和2010年4月30日。这将为您提供一个月的最后一天: function lastday($month = '', $year = '') { if (empty($month)) { $month = date('m'); } if (empty($year)) { $year = date('Y'); } $result = st
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函数查找一个月有多少天
// 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);
最简单的方法是使用
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'
)中的特定符号,并用其时间戳中的值替换它们。因此,我们可以使用这些符号从时间戳中提取所需的值和格式。在上述示例中:
提供从时间戳('2010'开始的4位年份)Y
给出时间戳后的数字月份,前导零('04')m
提供时间戳月份的天数(“30”)t
$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!
有关其他符号和更多详细信息,请参见。简单符号
- Y-年的完整数字表示形式,4位数字
- m-月份的数字表示形式,前导零
- t-给定月份的天数
获取上个月的第一个和最后一个日期
$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);
$dateBegin=strottime(“上个月的第一天”);
$dateEnd=strotime(“上个月的最后一天”);
回显日期(“D-F-Y”,$DATEBEANG);
回声“
”;
回显日期(“D-F-Y”,日期为$DATEND);
对于特定的月份和年份使用日期()作为自然语言,如下所示
$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'));
显示31日期的最后一天如果要从指定的日期变量中查找第一天和最后一天,则可以执行以下操作:
$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'));
格式简单
$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',strottime('1个月”)您可以使用:
$date=DateTimeImmutable::createFromFormat('Y-m-d','2021-02-13');
变量转储($date->modify('first day of this month')->格式('Y-m-d');//串(10)“2021-02-01”
变量转储($date->modify('本月最后一天')->格式('Y-m-d');//串(10)“2021-02-28”
可能重复:仅在此处添加信息时,我在使用此格式将日期转换回unix时间戳时遇到问题,已通过使用“d-m-Y”格式而不是“m-d-Y”格式修复。。最后一秒将是“23:59:59”,而不是“12:59:59”…您可以将日期(“Y-m-d”,strotime($date))替换为$date,因为它们是相同的,但您编写的代码较少。这个函数对我很有用。保存了我的一天。当你用任何月号替换m时,它不工作
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
$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);