php获取,昨,今,后天.... 本周,月,年...日期时间戳

时间戳->时间转换工具

时间戳(Unix timestamp)转换工具 - 在线工具 (tool.lu)

代码如下:

    public function date()
    {
        /** 日期 **/
        // 今天的日期
        $result['today_date'] = date("Y-m-d");
        // 昨天的日期
        $result['yesterday_date'] = date("Y-m-d", strtotime("-1 day"));
        // 前天的日期
        $result['before_yesterday_date'] = date('Y-m-d', strtotime('-2 days'));
        // 本周的起始日期(周一)
        $result['week_start_date'] = date('Y-m-d', strtotime('monday this week'));
        // 本周的结束日期(周日)
        $result['week_end_date'] = date('Y-m-d', strtotime('sunday this week'));
        // 本月的起始日期
        $result['month_start_date'] = date('Y-m-01');
        // 本月的结束日期
        $result['month_end_date'] = date('Y-m-t');
        // 本年的起始日期
        $result['year_start_date'] = date('Y-01-01');
        // 本年的结束日期
        $result['year_end_date'] = date('Y-12-31');
        // 明天的日期
        $result['tomorrow_date'] = date('Y-m-d', strtotime('+1 day', strtotime(date("Y-m-d"))));
        // 后天的日期
        $result['after_tomorrow_date'] = date('Y-m-d', strtotime('+2 days', strtotime(date("Y-m-d"))));
        // 7 天后的日期
        $result['seven_days_after_date'] = date('Y-m-d', strtotime('+7 days', strtotime(date("Y-m-d"))));
        // 1 个月后的日期
        $result['month_after_date'] = date('Y-m-d', strtotime('+1 month', strtotime(date("Y-m-d"))));
        // 1 年后的日期
        $result['year_after_date'] = date('Y-m-d', strtotime('+1 year', strtotime(date("Y-m-d"))));

        /** 时间戳 **/
        // 今天的时间戳
        $result['today_time'] = strtotime(date("Y-m-d"));
        // 昨天的时间戳
        $result['yesterday_time'] = strtotime("-1 day", strtotime(date("Y-m-d")));
        // 前天的时间戳
        $result['before_yesterday_time'] = strtotime("-2 days", strtotime(date("Y-m-d")));
        // 本周的起始日期(周一)的时间戳
        $result['week_start_time'] = strtotime('monday this week');
        // 本周的结束日期(周日)的时间戳
        $result['week_end_time'] = strtotime('sunday this week');
        // 本月的起始日期的时间戳
        $result['month_start_time'] = strtotime(date('Y-m-01'));
        // 本月的结束日期的时间戳
        $result['month_end_time'] = strtotime(date('Y-m-t'));
        // 本年的起始日期的时间戳
        $result['year_start_time'] = strtotime(date('Y-01-01'));
        // 本年的结束日期的时间戳
        $result['year_end_time'] = strtotime(date('Y-12-31'));
        // 明天的时间戳
        $result['tomorrow_time'] = strtotime('+1 day', strtotime(date("Y-m-d")));
        // 后天的时间戳
        $result['after_tomorrow_time'] = strtotime('+2 days', strtotime(date("Y-m-d")));
        // 7 天后的时间戳
        $result['seven_days_after_time'] = strtotime('+7 days', strtotime(date("Y-m-d")));
        // 1 个月后的时间戳
        $result['month_after_time'] = strtotime('+1 month', strtotime(date("Y-m-d")));
        // 1 年后的时间戳
        $result['year_after_time'] = strtotime('+1 year', strtotime(date("Y-m-d")));

        /** 区间段时间戳 **/
        // 今天的开始时间戳和结束时间戳
        $result['today_start_time'] = strtotime('today');
        $result['today_end_time']   = strtotime('tomorrow') - 1;

        // 昨天的开始时间戳和结束时间戳
        $result['yesterday_start_time'] = strtotime('yesterday');
        $result['yesterday_end_time']   = strtotime('today') - 1;

        // 前天的开始时间戳和结束时间戳
        $result['day_before_yesterday_start_time'] = strtotime('-2 days', strtotime('today'));
        $result['day_before_yesterday_end_time']   = strtotime('yesterday') - 1;

        // 上周的时间戳(从上周一到上周日)
        $result['last_week_start_time'] = strtotime('last week monday');
        $result['last_week_end_time']   = strtotime('last week sunday') + 86399; // 86399 秒是一天的最后一秒

        // 一个月前的时间戳(从一个月前的第一天到最后一天)
        $result['last_month_start_time'] = strtotime('first day of last month midnight');
        $result['last_month_end_time']   = strtotime('last day of last month 23:59:59'); // 最后一天的最后一秒

        // 明天的开始时间戳和结束时间戳
        $result['tomorrow_start_time'] = strtotime('tomorrow');
        $result['tomorrow_end_time']   = strtotime('tomorrow +1 day') - 1;

        // 后天的开始时间戳和结束时间戳
        $result['day_after_tomorrow_start_time'] = strtotime('+2 days midnight');
        $result['day_after_tomorrow_end_time']   = strtotime('+2 days 23:59:59');

        // 本周的开始时间戳和结束时间戳(从本周一到本周日)
        $result['this_week_start_time'] = strtotime('monday this week');
        $result['this_week_end_time']   = strtotime('sunday this week') + 86399;

        // 下周的开始时间戳和结束时间戳(从下周一到下周日)
        $result['next_week_start_time'] = strtotime('monday next week');
        $result['next_week_end_time']   = strtotime('sunday next week') + 86399;

        // 下个月的开始时间戳和结束时间戳(从下个月的第一天到最后一天)
        $result['next_month_start_time'] = strtotime('first day of next month midnight');
        $result['next_month_end_time']   = strtotime('last day of next month 23:59:59');

        dump($result);
        exit();
    }

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 12:24:05       7 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 12:24:05       8 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 12:24:05       7 阅读
  4. Python语言-面向对象

    2024-07-11 12:24:05       10 阅读

热门阅读

  1. 大话C语言:第28篇 内存分配与释放

    2024-07-11 12:24:05       6 阅读
  2. MySQL 中删除重复的数据并只保留一条

    2024-07-11 12:24:05       8 阅读
  3. spring boot 3.2.x 使用CDS加速启动

    2024-07-11 12:24:05       10 阅读
  4. 37.深度学习中的梯度下降法及其实现

    2024-07-11 12:24:05       9 阅读
  5. Spring Boot与Spring MVC的区别和联系

    2024-07-11 12:24:05       9 阅读
  6. 代码随想录-DAY⑥-哈希表——leetcode 383 | 454

    2024-07-11 12:24:05       9 阅读
  7. linux去掉行首的#字符

    2024-07-11 12:24:05       7 阅读
  8. 常见的负载均衡算法和实现方式

    2024-07-11 12:24:05       11 阅读
  9. Android焦点之Focused Window的更新(二)

    2024-07-11 12:24:05       8 阅读
  10. SpringBoot源码阅读(9)——转换服务

    2024-07-11 12:24:05       8 阅读
  11. C#中的Dictionary

    2024-07-11 12:24:05       9 阅读
  12. C语言标准库中的函数

    2024-07-11 12:24:05       9 阅读