• php 关于日期的一些计算


    1、strtotime基本使用

          date_default_timezone_set('PRC'); //设置中国时区 
          echo "今天:", date("Y-m-d", time()), "<br>";
          echo "昨天:", date("Y-m-d", strtotime("-1 day")), "<br>";
          echo "明天:", date("Y-m-d", strtotime("+1 day")), "<br>";
          echo "一周后:", date("Y-m-d", strtotime("+1 week")), "<br>";
          echo "一周零两天四小时两秒后:", date("Y-m-d G:H:s", strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
          echo "下个星期四:", date("Y-m-d", strtotime("next Thursday")), "<br>";
          echo "上个周一(大概率本周的周一):" . date("Y-m-d", strtotime("last Monday")) . "<br>";
          echo "上周一:" . date("Y-m-d", strtotime("-1 week last Monday")) . "<br>";
          echo "一个月前:" . date("Y-m-d", strtotime("2018-03-31 last month")) . "<br>";//一个月前:2018-03-03
          echo "一个月后:" . date("Y-m-d", strtotime("2018-01-31 +1 month")) . "<br>";//一个月前:2018-03-03
          echo "十年后:" . date("Y-m-d", strtotime("+10 year")) . "<br>";
    
          echo '<hr>';
          //省略一些参数
          echo date('Y-m-d H:i:s'). '<br/>';
          echo strtotime(date('Y')),'----',date('Y-m-d H:i:s',strtotime(date('Y'))). '<br/>';//?????? 20:18:00 从哪来的
          echo strtotime(date('Y-m')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m'))). '<br/>';//天 01,时分秒均为00
          echo strtotime(date('Y-m-d')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d'))). '<br/>';//时分秒均为00
          echo strtotime(date('Y-m-d H')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d H'))). '<br/>';//时间格式不识别
          echo strtotime(date('Y-m-d H:i')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i'))). '<br/>';//秒为00

    2、本月最后一天

          $date = '2018-2-17';
    
          echo '本月最后一天:' . date('Y-m-d', strtotime(date('Y-m-01', strtotime('+1 month'))) - 1) . '<br/>';
          echo '某月最后一天:' . date('Y-m-d', strtotime(date('Y-m-01', strtotime($date . ' +1 month'))) - 1) . '<br/>';
    
          //某月共多少天,可用于最后一天
          echo date('t', strtotime($date)) . '<br>';

    3、往后一个月 +1 month逻辑

    strtotime +1 month -1 month逻辑:先将日期转换成合法的年月日,如strtotime("2018-02-29 -1 month") 2018-02-29,转换为2018-03-01。+1 month操作为直接将月份+1,再转换为合法的年月日,如2018-01-31 月份加一个月为2018-02-31,转换为合法的日期就是2018-03-03

          echo "一个月后:" . date("Y-m-d", strtotime("2018-01-01 +1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-01-31 +1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-02-01 +1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-02-02 +1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-02-28 +1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-02-28 -1 month")) . "<br>";
          echo "一个月后:" . date("Y-m-d", strtotime("2018-02-29 -1 month")) . "<br>";

    4、获取一周中某天的日期(如周三)

          $date = '2018-2-17';
          var_dump(getOneDayInWeek(7, $date));
    
       /**
       * @desc 获得某一日期所在周(周一到周日)中的某天
       * @param int $search 查找星期几,周一 1,周二 2。。。周六 6,周日7
       * @param null $date 基准日期
       * @return bool|string
       */
    getOneDayInWeek($search, $date = NULL) {
        $date = !$date ? date('Y-m-d') : date('Y-m-d', strtotime($date));
        $search = intval($search);
        if ($search < 1 || $search > 7 || !$date) {
          return FALSE;
        }
        $weeks = ['', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
    
        $w = (int)date('w', strtotime($date));
        $w === 0 && $w = 7;
        $search > $w && $flag = ' next ';
        $search < $w && $flag = ' last ';
    
        return isset($flag) ? date("Y-m-d", strtotime($date . $flag . $weeks[$search])) : $date;
      }

    5、类似合同中下月减一天,试用期三个月后减一天

          $deviationMonth = 1;
          $deviationDay = -1;
          $date = '2018-10-1';
          var_dump(getNextMonth($date, $deviationMonth, $deviationDay));
    
      /**
       * @desc 类似合同中下月减一天,试用期三个月后减一天
       *  正常 2018-01-17 --- 2018-02-16,2017-10-01---2017-10-31,2017-10-31 --- 2017-11-30
       *  2018-01-28 --- 2018-02-27,2018-01-29 --- 2018-02-28,2018-01-30 --- 2018-02-28,2018-01-31 --- 2018-02-28,2018-02-01 --- 2018-02-28,2018-02-02 --- 2018-03-01,2018-02-03 --- 2018-03-02
       * @param null $date 起始日期
       * @param int $deviationMonth 向后推几个月
       * @param int $deviationDay 向后推几天,一般是向前一天(-1)
       * @return bool|string
       */
    getNextMonth($date = NULL, $deviationMonth = 1, $deviationDay = -1) {
        $timestamp = !$date ? time() : strtotime($date);
        $deviationMonth = intval($deviationMonth);
        $deviationDay = intval($deviationDay);
        if (!$deviationMonth || !$deviationDay || !$timestamp) {
          return FALSE;
        }
        $month = date('Y-m', strtotime(date('Y-m', $timestamp) . ' ' . $deviationMonth . ' month'));
        $day = date('d', $timestamp) + $deviationDay;
    
        $day > date('t', strtotime($month)) && $day = date('t', strtotime($month));
    
        return date('Y-m-d', strtotime($month . '-' . $day));
      }

    参考

    PHP的日期操作 增加减少

    php 如何计算指定时间,往后1个月的时间戳

  • 相关阅读:
    XML 验证器
    XML 浏览器支持
    XML 元素
    查看 XML 文件
    XML 属性
    Android入门之Activity四种启动模式
    XML 简介
    XML 语法规则
    [Android]Thread线程入门3多线程
    XML 验证
  • 原文地址:https://www.cnblogs.com/fps2tao/p/9505250.html
Copyright © 2020-2023  润新知