php 如何使用 Carbon 获取上个月的第一天和最后一天 - Laravel

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

How to get First and Last Day of Previous Month with Carbon - Laravel

phplaraveldatephp-carbon

提问by Siddharth

I need Firstand Last Dayof Previous Monthusing Carbon Library, what I have tried is as follows:

我需要首先最后一天前一个月使用的碳库,我曾尝试如下:

$firstDayofPreviousMonth = Carbon::now()->startOfMonth()->subMonth()->toDateString();
$lastDayofPreviousMonth = Carbon::now()->endOfMonth()->subMonth()->toDateString();

Result I'm getting is for$firstDayofPreviousMonth = '2016-04-01'(as current month is 5th(May)) and for $lastDayofPreviousMonth = '2016-05-01'.

我得到的结果是$firstDayofPreviousMonth = '2016-04-01'(因为当月是 5 日(5 月))和$lastDayofPreviousMonth = '2016-05-01'.

I'm getting correct result for $firstDayofPreviousMonth, but it's giving me 30 days previous result, and giving me wrong result for $lastDayofPreviousMonth.

我得到了正确的结果$firstDayofPreviousMonth,但它给了我 30 天前的结果,并给了我错误的结果$lastDayofPreviousMonth

Can anyone help me out with this?

谁能帮我解决这个问题?

回答by Deniz B.

Try this:

尝试这个:

$start = new Carbon('first day of last month');
$end = new Carbon('last day of last month');

回答by Abu Sayem

Just try this

试试这个

$firstDayofPreviousMonth = Carbon::now()->startOfMonth()->subMonth()->toDateString(); $lastDayofPreviousMonth = Carbon::now()->subMonth()->endOfMonth()->toDateString();

$firstDayofPreviousMonth = Carbon::now()->startOfMonth()->subMonth()->toDateString(); $lastDayofPreviousMonth = Carbon::now()->subMonth()->endOfMonth()->toDateString();

回答by sadalsuud

With this ... the date start init on 00:00 and date end finish in 23:59

有了这个......日期开始于 00:00,日期结束于 23:59

$start = new Carbon('first day of last month');
$start->startOfMonth();
$end = new Carbon('last day of last month');
$end->endOfMonth();

回答by Tama

To specifically answer your question as to why you're getting the wrong result for $lastDayofPreviousMonth.

专门回答你的问题,为什么你得到了错误的结果$lastDayofPreviousMonth

Lets break down this statement in your example:

让我们在您的示例中分解此语句:

Carbon::now()->endOfMonth()->subMonth()->toDateString();
// Carbon::now() > 2016-05-05
// ->endOfMonth() > 2016-05-31
// ->subMonth() > 2016-04-31 // Simply takes 1 away from 5.

This leaves us with an invalid date — there is no 31st of April. The extra day is simply added on to the last valid date (2016-04-30 + 1) which rolls the date into May (2016-05-01).

这给我们留下了一个无效的日期——没有 4 月 31 日。额外的一天只是添加到最后一个有效日期 (2016-04-30 + 1) 上,该日期将日期滚动到五月 (2016-05-01)。

As previously mentioned to be sure this never happens always reset the date to the 1st of the month before doing anything else (as every month has a 1st day).

如前所述,为了确保这永远不会发生,在做任何其他事情之前总是将日期重置为一个月的第一天(因为每个月都有第一天)。

$lastDayofPreviousMonth = Carbon::now()->startofMonth()->subMonth()->endOfMonth()->toDateString();
// Carbon::now() > 2016-05-05
// ->startofMonth() > 2016-05-01 00:00:00
// ->subMonth() > 2016-04-01 00:00:00
// ->endOfMonth() > 2016-04-30 23:59:59

回答by FuzzyJared

There is currently a bug within Carbon when using the method

使用该方法时,Carbon 中目前存在一个错误

Carbon::now()->startOfMonth()->subMonth()->endOfMonth()->toDateTimeString();

The bug results in returning the last day as 30 for those months that have 31 days.

该错误导致那些有 31 天的月份的最后一天返回 30。

IE - if you are in March and you run the above call it will return 2017-03-30 and not 2017-03-31 as you would expect.

IE - 如果您在 3 月份并且运行上述调用,它将返回 2017-03-30 而不是 2017-03-31,正如您所期望的。

As I was doing a between dates operation I ended up using..

当我在做一个日期之间的操作时,我最终使用了..

Carbon::now()->startOfMonth()->subSeconds(1)->toDateTimeString();

This ended up with the correct date dateTimeString for those days that end on the 31st.

这最终得到了在 31 日结束的那些日子的正确日期 dateTimeString。

回答by elena

Another solution is using Carbon method subMonthNoOverflow():

另一种解决方案是使用 Carbon 方法subMonthNoOverflow()

$lastDayofPreviousMonth = Carbon::now()->subMonthNoOverflow()->endOfMonth()->toDateString();

Found it here when run into 31 day of month issue: https://github.com/briannesbitt/Carbon/issues/627

在遇到每月 31 天的问题时在这里找到它:https: //github.com/briannesbitt/Carbon/issues/627