如何获得一个月的第一天和最后一天

时间:2022-10-16 12:25:00

I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a 'Y-m-d' parameter format, but I don't know how can I get the last day of the given month date.

我想重写一个mysql查询,它使用month()和year()函数来显示某个月的所有文章,这些文章以“Y-m-d”参数格式显示在我的函数中,但是我不知道如何获得给定月份的最后一天。

$query_date = '2010-02-04';
list($y, $m, $d) = explode('-', $query_date);
$first_day = $y . '-' . $m . '-01';

7 个解决方案

#1


105  

You might want to look at the strtotime and date functions.

您可能需要查看strtotime和date函数。

<?php

$query_date = '2010-02-04';

// First day of the month.
echo date('Y-m-01', strtotime($query_date));

// Last day of the month.
echo date('Y-m-t', strtotime($query_date));

#2


6  

Try this , if you are using PHP 5.3+, in php

如果在PHP中使用PHP 5.3+,试试这个。

$query_date = '2010-02-04';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');

For finding next month last date, modify as follows,

为了寻找下个月的最后一个月,修改如下,

 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');

and so on..

等等. .

#3


5  

I know this question has a good answer with 't', but thought I would add another solution.

我知道这个问题有一个很好的答案是“t”,但是我想我可以添加另一个解决方案。

$first = date("Y-m-d", strtotime("first day of this month"));
$last = date("Y-m-d", strtotime("last day of this month"));

#4


4  

cal_days_in_month() should give you the total number of days in the month, and therefore, the last one.

cal_days_in_month()应该给出一个月的总天数,因此是最后一天。

#5


1  

Basically:

基本上:

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

Date t parameter return days number in current month.

t参数返回日期为当月天数。

#6


1  

// First date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
echo '<br />';
// Last date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m')+1, 0, date('Y')));

#7


1  

$month = 10; // october

$firstday = date('01-' . $month . '-Y');
$lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');

#1


105  

You might want to look at the strtotime and date functions.

您可能需要查看strtotime和date函数。

<?php

$query_date = '2010-02-04';

// First day of the month.
echo date('Y-m-01', strtotime($query_date));

// Last day of the month.
echo date('Y-m-t', strtotime($query_date));

#2


6  

Try this , if you are using PHP 5.3+, in php

如果在PHP中使用PHP 5.3+,试试这个。

$query_date = '2010-02-04';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');

For finding next month last date, modify as follows,

为了寻找下个月的最后一个月,修改如下,

 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');

and so on..

等等. .

#3


5  

I know this question has a good answer with 't', but thought I would add another solution.

我知道这个问题有一个很好的答案是“t”,但是我想我可以添加另一个解决方案。

$first = date("Y-m-d", strtotime("first day of this month"));
$last = date("Y-m-d", strtotime("last day of this month"));

#4


4  

cal_days_in_month() should give you the total number of days in the month, and therefore, the last one.

cal_days_in_month()应该给出一个月的总天数,因此是最后一天。

#5


1  

Basically:

基本上:

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

Date t parameter return days number in current month.

t参数返回日期为当月天数。

#6


1  

// First date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
echo '<br />';
// Last date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m')+1, 0, date('Y')));

#7


1  

$month = 10; // october

$firstday = date('01-' . $month . '-Y');
$lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');