Php 如何获取给定月份的第一天和最后一天

Php 如何获取给定月份的第一天和最后一天,php,date,Php,Date,我想重写一个mysql查询,它使用month()和year()函数来显示某个月的所有帖子,这些帖子以“Y-m-d”参数格式发送到我的函数,但我不知道如何获取给定月份日期的最后一天 $query_date = '2010-02-04'; list($y, $m, $d) = explode('-', $query_date); $first_day = $y . '-' . $m . '-01'; 您可能需要查看和函数 <?php $query_date = '2010-02-04';

我想重写一个mysql查询,它使用month()和year()函数来显示某个月的所有帖子,这些帖子以“Y-m-d”参数格式发送到我的函数,但我不知道如何获取给定月份日期的最后一天

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

您可能需要查看和函数

<?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));
基本上:

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

Date t参数返回当前月份的天数。

应该提供该月份的总天数,因此是最后一个。

我知道这个问题的答案是“t”,但我想我会添加另一个解决方案

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

如果您使用的是PHP 5.3+,请在PHP中尝试此方法

$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');
要查找下个月的最后日期,请修改如下:

 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');
依此类推。

//当前日期的第一个日期
// 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')));
回音日期('Y-m-d',mktime(0,0,0,日期('m'),1,日期('Y')); 回声“
”; //当前日期的最后日期 回音日期('Y-m-d',mktime(0,0,0,日期('m')+1,0,日期('Y'));
仅打印当月周:

function my_week_range($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
    $start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
    if($currentWeek==1)
        {$start_date = date('Y-m-01', strtotime($date));}
    else if($currentWeek==5)
       {$end_date = date('Y-m-t', strtotime($date));}
    else
       {}
    return array($start_date, $end_date );
}

$date_range=list($start_date, $end_date) = my_week_range($new_fdate);
##获取当月的第一个日期和最后一个日期
回声“今日日期:”$查询日期=日期('d-m-Y');
echo“
月的第一天:”。日期('01-m-Y',标准时间('query_date'); 回声“
月的最后一天:”。日期('t-m-Y',标准时间($query_date));
谢谢分享。我以前从未使用过日历PHP模块。话虽如此,值得指出的是,该模块可能不会安装,尤其是在共享主机上。ty:你的方法更聪明。不需要分解。如果$query_date以“d-m-Y”格式来自php,会怎样:“02-04-2010”?在我的测试中,一切顺利,但也许我没有测试所有的情况。我是否必须转换为Y-m-d或strotime,请理解它。支持“d-m-Y”格式。支持的格式列在。我找不到“t”值的定义位置。你能给我指一下正确的方向吗?我已经在网上搜索了很多次了。这是一个巧妙的解决办法。谢谢Francois。当用现有答案为旧问题添加答案时,解释你的答案给问题带来了什么新的方面是很有用的。如果时间的流逝影响了答案,也要承认这一点。这个解决方案是错误的,因为问题是关于“给定的月份”,但你的答案只适用于当前月份。
function my_week_range($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
    $start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
    if($currentWeek==1)
        {$start_date = date('Y-m-01', strtotime($date));}
    else if($currentWeek==5)
       {$end_date = date('Y-m-t', strtotime($date));}
    else
       {}
    return array($start_date, $end_date );
}

$date_range=list($start_date, $end_date) = my_week_range($new_fdate);
## Get Current Month's First Date And Last Date

echo "Today Date: ". $query_date = date('d-m-Y');
echo "<br> First day of the month: ". date('01-m-Y', strtotime($query_date));
echo "<br> Last day of the month: ". date('t-m-Y', strtotime($query_date));