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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7541938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:55:12  来源:igfitidea点击:

how to get the first and last days of a given month

phpdate

提问by dole doug

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() 函数来显示某个月份的所有帖子,这些帖子作为“Ymd”参数格式进入我的函数,但我不知道如何获得给定月份日期的最后一天。

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

回答by Francois Deschenes

You might want to look at the strtotimeand datefunctions.

您可能想查看strtotimedate函数。

<?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));

回答by Goldbug

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"));

回答by Mohammed Safeer

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

试试这个,如果你使用的是 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');

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..

等等..

回答by Jonnix

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

cal_days_in_month()应该给你当月的总天数,因此,最后一天。

回答by neworld

Basically:

基本上:

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

Datet parameter return days number in current month.

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

回答by J Ajay

// 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')));

回答by Mevlüt?zdemir

$month = 10; // october

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

回答by kayal

Print only current month week:

仅打印当月周:

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);

回答by PHP Kishan

## 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));