urv*_*sha 4 php datetime laravel php-carbon
在 laravel 5.6 中使用 Carbon。
我想编写一个代码,为我提供当前日期中下一次出现的日期。
例如给出下一个 5 月 31 日的日期
场景 1:
输入:$currentDate = '01-30-2019'; // MM-DD-YYYY 格式
预期输出:$next31May = '05-31-2019';
场景 2:
输入:$currentDate = '07-04-2019'; // MM-DD-YYYY 格式
预期输出:$next31May = '05-31-2020';
更新:
我尝试了下面的代码但不满足
<?php
public function nextOccurance()
{
$now = Carbon::now();
$month= $now->month;
$year = $now->year;
if($month > 6)
{
echo Carbon::createMidnightDate($year+1, 5, 31);
}
else
{
echo Carbon::createMidnightDate(null, 5, 31);
}
exit();
}
?>
Run Code Online (Sandbox Code Playgroud)
先感谢您。
public function nextOccurance()
{
// the 31th of May of the current year
$day = Carbon::createFromFormat('m-d', '05-31');
$now = Carbon::now();
// If today after $day
if($now >= $day) {
// Gat a next year
$day->modify('next year');
}
echo $day->format('Y-m-d');
exit();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6585 次 |
| 最近记录: |