在 PHP 中,有没有一种简单的方法来获取一个月的第一个和最后一个日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43589/
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-24 21:17:38  来源:igfitidea点击:

In PHP, is there an easy way to get the first and last date of a month?

php

提问by Thomas Owens

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?

我需要以 YYYY-MM-DD 格式获取一个月的第一天和最后一天,仅给出月份和年份。有没有好的,简单的方法来做到这一点?

回答by Micha? Rudnicki

$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));

See date()in PHP documentation.

请参阅PHP 文档中的date()

回答by Biri

First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))

第一天总是YYYY-MM-01,不是吗?例子:date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))

Last day is the previous day of the next month's first day:

最后一天是下个月第一天的前一天:

$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");

回答by Niyaz

The first day of the month is always 1. So it will become

这个月的第一天总是 1。所以它会变成

YYYY-MM-01

the last day can be calculated as:

最后一天可以计算为:

<?php
    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
    echo "There was $num days in August 2003";
?>

回答by ZombieSheep

OK, first is dead easy.

好的,首先很容易。

date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));

Last is a little trickier, but not much.

最后一个有点棘手,但不是很多。

date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));

If I remember my PHP date stuff correctly...

如果我正确记住了我的 PHP 日期内容...

**edit - Gah! Beaten to it about a million times...

**编辑 - 啊!被它打了大约一百万次......

Edit by Pat:

帕特编辑:

Last day should have been

最后一天应该是

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1

回答by Eranda

<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>

回答by Eranda

The easiest way to do this with PHP is

使用 PHP 执行此操作的最简单方法是

$dateBegin = strtotime("first day of last month");  
$dateEnd = strtotime("last day of last month");

echo date("MYDATEFORMAT", $dateBegin);  
echo "<br>";        
echo date("MYDATEFORMAT", $dateEnd);

Or the last week

或者上周

if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week"); 
$dateEnd = strtotime("Sunday last week");   
}

Or the last year

或者去年

$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");

回答by Nate

try this to get the number of days in the month:

试试这个来获得当月的天数:

$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));

回答by csonuryilmaz

Example; I want to get first day and last day of current month.

例子; 我想获得当月的第一天和最后一天。

$month   = (int) date('F');
$year    = (int) date('Y');

date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last

When you run this for instance at date 2015-01-09, the first and last values will be sequentially;

例如,当您在 2015-01-09 日期运行它时,第一个和最后一个值将按顺序排列;

2015-01-01
2015-01-31

Tested.

测试。

回答by arturwwl

From here(get next month last day)that is marked as duplicated, so i can't add comment there, but people can got bad answers from there.

这里(最后一天获取下个月)标记为重复,所以我无法在那里添加评论,但人们可以从那里得到错误的答案。

Correct one for last day of next month:

更正下个月最后一天的一个:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));

Correct one for first day of next month:

更正下个月第一天的一个:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));

Code like this will be providing March from January, so that's not what could be expected.

像这样的代码将从一月开始提供三月,所以这不是预期的。

echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));

echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));

回答by Pat

By the way @ZombieSheep solution

顺便说一下@ZombieSheep 解决方案

date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));

does not work it should be

不工作它应该是

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1

Of course @Micha? S?aby's accepted solution is the simplest.

当然@Micha?S?aby 接受的解决方案是最简单的。