zoukankan      html  css  js  c++  java
  • php获取指定日期,前一天、前一周、前一个月、前一年,后一天,后一周,前一个月,前一年

    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;
    }
    
  • 相关阅读:
    与客服聊天功能测试点
    京东优惠券如何测试
    Linux笔试题
    线程与线程池原理
    PyCharm 介绍、安装、入门使用
    银行APP测试用户体验性方面
    python的闭包
    列表解析2
    深入函数
    再谈装饰器@@@
  • 原文地址:https://www.cnblogs.com/pcx105/p/10328965.html
Copyright © 2011-2022 走看看