How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010.
如何使用PHP找到一个月内的第一个和最后一个日期?例如,今天是2010年4月21日;我想找到2010年4月1日和2010年4月30日。
9 个解决方案
#1
171
The easiest way is to use date
, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.
最简单的方法是使用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()
searches the string it's given, like 'm-t-Y'
, for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:
date()搜索给定的字符串,如“m-t-Y”,查找特定的符号,并使用来自其时间戳的值替换它们。我们可以使用这些符号从时间戳中提取我们想要的值和格式。在上面的例子中:
-
Y
gives you the 4-digit year from the timestamp ('2010') - Y给你的是时间戳的4位数年份(2010年)
-
m
gives you the numeric month from the timestamp, with a leading zero ('04') - m给出来自时间戳的数字月,以0开头(“04”)
-
t
gives you the number of days in the timestamp's month ('30') - t给出时间戳月份的天数(“30”)
You can be creative with this. For example, to get the first and last second of a month:
你可以有创意。例如,获得一个月的第一和最后一秒:
$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!
See http://php.net/manual/en/function.date.php for other symbols and more details.
参见http://php.net/manual/en/function.date.php查看其他符号和更多细节。
#2
21
Simple one
简单的一个
- Y - A full numeric representation of a year, 4 digits
- Y -一年的完整数字表示,4位数字
- m - Numeric representation of a month, with leading zeros
- m -一个月的数字表示,以0开头
- t - Number of days in the given month
- t -给定月份的天数
Reference - http://www.php.net/manual/en/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 />';
?>
#3
14
You can use the date function to find how many days in a month there are.
您可以使用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.
Hope that helps.
希望有帮助。
EDIT: After reading Luis' answer, it occurred to me you may want it in the right format (YY-mm-dd). It may be obvious, but doesn't hurt to mention:
编辑:在阅读了Luis的答案后,我突然想到你可能想要正确的格式(YY-mm-dd)。这可能是显而易见的,但不妨提一下:
// After the above code
echo date('Y-m-t', $ts);
#4
9
This will give you the last day of the month:
这将给你一个月的最后一天:
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);
}
And the first Day:
第一天:
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);
}
#5
3
For specific month and year use date() as natural language as following
对于特定的月份和年份使用日期()作为自然语言如下。
$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?
but for Current month
但在本月
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));
#6
3
If you want to find the first day and last day from the specified date variable then you can do this like below:
如果您想从指定的日期变量中找到第一天和最后一天,您可以这样做:
$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);
For the current date just simple use this
对于当前日期,只需使用这个
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
现场演示
#7
2
To get the first and last date of Last 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);
#8
2
In simple format
简单的格式
$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);
For next month change $curMonth to $curMonth = date('F',strtotime("+1 months"));
下个月将$curMonth改为$curMonth = date(“F”,strtotime(“+1个月”));
#9
0
$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;
display 31 last day of date
显示日期的最后一天
#1
171
The easiest way is to use date
, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.
最简单的方法是使用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()
searches the string it's given, like 'm-t-Y'
, for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:
date()搜索给定的字符串,如“m-t-Y”,查找特定的符号,并使用来自其时间戳的值替换它们。我们可以使用这些符号从时间戳中提取我们想要的值和格式。在上面的例子中:
-
Y
gives you the 4-digit year from the timestamp ('2010') - Y给你的是时间戳的4位数年份(2010年)
-
m
gives you the numeric month from the timestamp, with a leading zero ('04') - m给出来自时间戳的数字月,以0开头(“04”)
-
t
gives you the number of days in the timestamp's month ('30') - t给出时间戳月份的天数(“30”)
You can be creative with this. For example, to get the first and last second of a month:
你可以有创意。例如,获得一个月的第一和最后一秒:
$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!
See http://php.net/manual/en/function.date.php for other symbols and more details.
参见http://php.net/manual/en/function.date.php查看其他符号和更多细节。
#2
21
Simple one
简单的一个
- Y - A full numeric representation of a year, 4 digits
- Y -一年的完整数字表示,4位数字
- m - Numeric representation of a month, with leading zeros
- m -一个月的数字表示,以0开头
- t - Number of days in the given month
- t -给定月份的天数
Reference - http://www.php.net/manual/en/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 />';
?>
#3
14
You can use the date function to find how many days in a month there are.
您可以使用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.
Hope that helps.
希望有帮助。
EDIT: After reading Luis' answer, it occurred to me you may want it in the right format (YY-mm-dd). It may be obvious, but doesn't hurt to mention:
编辑:在阅读了Luis的答案后,我突然想到你可能想要正确的格式(YY-mm-dd)。这可能是显而易见的,但不妨提一下:
// After the above code
echo date('Y-m-t', $ts);
#4
9
This will give you the last day of the month:
这将给你一个月的最后一天:
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);
}
And the first Day:
第一天:
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);
}
#5
3
For specific month and year use date() as natural language as following
对于特定的月份和年份使用日期()作为自然语言如下。
$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?
but for Current month
但在本月
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));
#6
3
If you want to find the first day and last day from the specified date variable then you can do this like below:
如果您想从指定的日期变量中找到第一天和最后一天,您可以这样做:
$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);
For the current date just simple use this
对于当前日期,只需使用这个
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
现场演示
#7
2
To get the first and last date of Last 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);
#8
2
In simple format
简单的格式
$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);
For next month change $curMonth to $curMonth = date('F',strtotime("+1 months"));
下个月将$curMonth改为$curMonth = date(“F”,strtotime(“+1个月”));
#9
0
$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;
display 31 last day of date
显示日期的最后一天