简体   繁体   English

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

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

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year.我需要以 YYYY-MM-DD 格式获取一个月的第一天和最后一天,仅给出月份和年份。 Is there a good, easy way to do this?有没有好的,简单的方法来做到这一点?

$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()

First day is always YYYY-MM-01, isn't it? 第一天总是YYYY-MM-01,不是吗? Example: date("YMd", mktime(0, 0, 0, 8, 1, 2008)) 示例: date("YMd", 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");

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

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

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

尝试此操作以获取当月的天数:

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

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年1月9日运行此代码时,第一个和最后一个值将是顺序的;

2015-01-01
2015-01-31

Tested. 经过测试。

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接受的解决方案是最简单的。

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('Ym-t'));

Just to verify that I didn't miss any loose ends: 只是为了确认我没有错过任何结局:

$startDay = 1;

if (date("m") == 1) {
    $startMonth = 12;
    $startYear = date("Y") - 1;

    $endMonth = 12;
    $endYear = date("Y") - 1;
}
else {
    $startMonth = date("m") - 1;
    $startYear = date("Y");

    $endMonth = date("m") - 1;
    $endYear = date("Y");
}

$endDay = date("d") - 1;

$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));

proper way to build a relative date from now is:从现在开始建立相对日期的正确方法是:

//bad example - will be broken when generated at 30 of December (broken February)
    echo date("Y-m-d", strtotime("now"))."\n";
    echo date("Y-m-d", strtotime("now + 1 month"))."\n";
    echo date("Y-m-d", strtotime("now + 2 month"))."\n";
    echo date("Y-m-d", strtotime("now + 3 month"))."\n";
    
//good example, you can change first day to last day or any day
    echo date("Y-m-d", strtotime("first day of this month"))."\n";
    echo date("Y-m-d", strtotime("first day of next month"))."\n";
    echo date("Y-m-d", strtotime("first day of +2 month"))."\n";
    echo date("Y-m-d", strtotime("first day of +3 month"))."\n";

and the result will be:结果将是:

2021-12-30
2022-01-30
2022-03-02
2022-03-30

2021-12-01
2022-01-01
2022-02-01
2022-03-01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM