php - PHP获取上个月最后一天和下个月第一天
问题描述
如果我有例如:
'2020-01-01'
我想要两个日期:
- 上个月的最后一天:
'2019-12-31'
- 下个月的第一天:
'2020-02-01'
我尝试使用类似的东西,echo date("Y-n-31", strtotime('2020-01-01'));
但我不知道。
谢谢你。
解决方案
使用以下代码:
<?php
$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");
echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month
?>
使用具有用户定义日期的 Datetime 对象(自定义日期)
# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";
$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";
Output:
30.11.2019
01.01.2020
推荐阅读
- java - WFLYEJB0467:请求被拒绝,因为容器被挂起
- java - 为什么我不能更改 JButton(禁用)文本颜色?
- sql-server - 从所有数据库中选择相同的表名
- angular - 在 Angular 模板中使用属性绑定时,类型“字符串”不可分配给类型“数字”
- xamarin.forms - 当我们使用分层导航页面 A 到页面 B 和页面 C 时。当我们按下按钮页面 C 时,它应该重定向到主页
- laravel - InvalidArgumentException 验证规则唯一需要至少 1 个参数
- flutter - 透明卡片,但带有高程阴影效果
- typescript - 如何在我的 TypeScript 类中创建强类型事件发射器?
- python - 两个图像是否有可能具有相同的像素值但仍然不同?
- javascript - 无法安装 ESlint 和 Prittier 依赖项?