php获取指定日期,前一天、前一周、前一个月、前一年,后一天,后一周,前一个月,前一年
2024-08-26 14:13:07
dump(date('Y-m-d', strtotime('2018-10-1 +1 day'))); dump(date('Y-m-d', strtotime('2018-10-1 +1 week'))); dump(date('Y-m-d', strtotime('2018-10-1 +1 month'))); dump(date('Y-m-d', strtotime('2018-10-1 +1 year'))); dump(date('Y-m-d', strtotime('2018-10-1 -1 day'))); dump(date('Y-m-d', strtotime('2018-10-1 -1 week'))); dump(date('Y-m-d', strtotime('2018-10-1 -1 month'))); dump(date('Y-m-d', strtotime('2018-10-1 -1 year')));
同理,把2018-10-1换成变量,就可以做很多事了!
ps 由于每个月份的天数不一样 +/- 1 month 特殊日期会有误差
//获取上一个月日期方法
public function getBeforeMonthDate($currentDate)
{
$currentMonthDay = date('t', strtotime($currentDate));
$currentDay = date('d', strtotime($currentDate));
$yearMonth = date('Y-m', strtotime($currentDate));
$beforeMonth = date('Y-m-d', strtotime("$yearMonth -1 month"));
$beforeMonthDay = date('t', strtotime($beforeMonth));
if ($beforeMonthDay < $currentMonthDay && $beforeMonthDay < $currentDay) {
$beforeDate = date("Y-m-d", strtotime("last day of -1 month", strtotime($currentDate)));
} else {
$beforeDate = date('Y-m-d', strtotime("$currentDate -1 month"));
}
return $beforeDate;
}
最新文章
- scheduletask任务调度
- js闭包
- js 利用throw 写的一个小程序
- 键盘和alertView的冲突问题
- http 错误 500.21
- (转)RSA算法原理(二)
- Tomcat上的项目部署到WebLogic上の注意事项
- jansen字符串转换为xml格式
- ASP.Net自定义重写Http Server标头
- 关于javascript的slice方法
- Activity 与ListActivity的区别
- ios创建的sqlite数据库文件如何从ios模拟器中导出
- SqlServer sys.partition_view
- cmd正常启动tomcat,而 从eclipse启动出现 404
- 关于启动调试时,总是启动多个web端口的问题
- 1593: [Usaco2008 Feb]Hotel 旅馆
- iOS 科学计数法保留N位有效数字
- 【转】Jenkins 安装与配置
- 【一天一道LeetCode】#97. Interleaving String
- 五种IO模型透彻分析