简体   繁体   English

PHP 或 Laravel 下个月的最后一天

[英]Last day of next month in PHP or Laravel

When I try to get the last day of next month by PHP or Laravel carbon, everything is ok, but when I try to get the last day of next month from a specific date, 2023-01-31, by当我尝试通过 PHP 或 Laravel 碳获取下个月的最后一天时,一切正常,但是当我尝试从特定日期 2023-01-31 获取下个月的最后一天时,通过

date('Y-m-01', strtotime('next month', strtotime('2023-01-31')));

OR或者

Carbon::createFromFormat('Y-m-d H:m:00:000', '2023-01-31')->addMonth()->format('Y-m-t H:m:00:000');

That time output results are given on 2023-03-31, but the last day of next month is 2023-02-28.那个时候output结果是2023-03-31给出的,但是下个月的最后一天是2023-02-28。 So how can I solve it?那么我该如何解决呢? It's not working correctly.它无法正常工作。

$instituter = Institute::where('code', $instInfo->institute_code)->first();
            // $institute = Carbon::createFromFormat('Y-m-d H:m:00:000', $institute->institute_expire)->addMonth()->format('Y-m-d H:m:00:000');
            // $expire = $instituter->institute_expire;
            // $inst = Carbon::createFromFormat('Y-m-d H:i:00.000', $instituter->institute_expire)->addMonths(6)->startOfMonth()->format('Y-m-d');
            $inst = date('Y-m-01', strtotime('next month', strtotime($instituter->institute_expire)));
            // $inst = Carbon::createFromFormat('Y-m-d H:i:00.000', $instituter->institute_expire)->addMonths(6)->startOfMonth();
            // $institute = Carbon::createFromFormat('m/d/Y', $instituter->institute_expire)->addMonth();

Try the following.尝试以下操作。

$date = Carbon::createFromFormat('Y-m-d', '2023-01-31');
$nextMonth = $date->modify('Last Day of Next Month');
echo $nextMonth->format('Y-m-d');

This will output 2023-02-28 .这将是 output 2023-02-28

A date is given as a string.日期以字符串形式给出。

$strDate = '2023-01-15';

Based on this date, a date object is to be created for the last day of the following month.基于此日期,将为下个月的最后一天创建日期 object。 With DateTime this can be done in one line.使用 DateTime,这可以在一行中完成。

$lastDdayOfNextMonth = date_create('last day of next month '.$strDate);

Carbon is an extension of DateTime. Carbon 是 DateTime 的扩展。 Therefore this also works:因此这也适用:

$lastDdayOfNextMonth = Carbon::create('last day of next month '.$strDate);

echo $lastDdayOfNextMonth->format('c');
//2023-02-28T00:00:00+01:00

If the date already exists as an object, it becomes even easier.如果日期已经作为 object 存在,那就更容易了。

$dt = new DateTime('2023-01-15');

$lastDdayOfNextMonth = $dt->modify('last day of next month');

This works exactly the same with carbon again.这再次与碳完全相同。

$dt = new Carbon('2023-01-15');

$lastDdayOfNextMonth = $dt->modify('last day of next month');

Beware of solutions with addMonth() with Carbon or just 'next Month' for DateTime.提防带有 Carbon 的 addMonth() 解决方案或 DateTime 的“下个月”解决方案。 Older versions also return incorrect results for the date "2023-01-31" as with the question.与问题一样,旧版本也会返回日期“2023-01-31”的错误结果。 Carbon has a special method for adding months 'without overflow': addMonthsNoOverflow() Carbon 有一个特殊的方法来添加“没有溢出”的月份: addMonthsNoOverflow()

$endOfNextMonth = Carbon::create('2023-01-31')
  ->addMonthsNoOverflow(1)
  ->endOfMonth()
;

echo $endOfNextMonth;  //2023-02-28 23:59:59

From Laravel 5.5 you can use now() function to get the current date and time and you can get the last date of next month using:从 Laravel 5.5 开始,您可以使用 now() function 获取当前日期和时间,您可以使用以下方法获取下个月的最后一个日期:

now()->addMonth()->endOfMonth();

if you want to format to Ymd如果你想格式化为 Ymd

now()->addMonth()->endOfMonth()->format('Y-m-d');

or if you want to get only date change format to $now->format('d');或者如果你只想获得日期更改格式 $now->format('d');

if you are using laravel version less than 5.5 using the above functions as如果您使用的是 laravel 版本低于 5.5 使用上述功能作为

\Carbon\Carbon::now()->addMonth()->endOfMonth();

strtotime will do this... strtotime 会这样做...

eg例如

echo date('c', strtotime('last day of next month'));

gives eg 2023-02-28T19:53:34+00:00, when today is 2023/01/21.给出例如 2023-02-28T19:53:34+00:00,而今天是 2023/01/21。

Alternatively -或者 -

ècho (new DateTime('last day of next month'))->format('c')

will also work也会工作

$next_month_last_date = date("Y-m-t", strtotime("+1 month"));

t => number of days in the month t => 该月的天数

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

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