php中怎么实现一个时间助手类

发布时间:2021-06-29 17:13:36 作者:Leah
来源:亿速云 阅读:87

今天就跟大家聊聊有关php中怎么实现一个时间助手类,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<?php


namespace Helper;

use app\common\model\Holiday;


class DateHelper
{
    /**
     * PHP计算两个时间段是否有交集(边界重叠不算)
     * @param string $beginTime1 开始时间1
     * @param string $endTime1 结束时间1
     * @param string $beginTime2 开始时间2
     * @param string $endTime2 结束时间2
     * @return bool
     */
    public static function isTimeCross($beginTime1 = '', $endTime1 = '', $beginTime2 = '', $endTime2 = ''){
        $status = $beginTime2 - $beginTime1;
        if ($status > 0){
            $status2 = $beginTime2 - $endTime1;
            if ($status2 > 0){
                return false;
            }elseif ($status2 < 0){
                return true;
            }else{
                return false;
            }
        }elseif($status < 0){
            $status2 = $endTime2 - $beginTime1;
            if ($status2 > 0){
                return true;
            }else if ($status2 < 0){
                return false;
            }else{
                return false;
            }
        }else{
            $status2 = $endTime2 - $beginTime1;
            if ($status2 == 0){
                return false;
            }else{
                return true;
            }
        }
    }

    /**
     * 时间天数差
     * @param $day1
     * @param $day2
     * @return float|int
     */
    public static function diffBetweenTwoDays($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp     = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ceil($second1 - $second2) / 86400;
    }

    /**
     * 时间格式化
     * @param $date
     * @param string $format
     * @return false|string
     */
    public static function dataFormat($date, $format = 'Y-m-d')
    {
        $date = is_numeric($date) ? $date : strtotime($date);
        return !$format ? $date : date($format, $date);

    }

    /**
     * 是否工作日
     * @param $date
     * @return bool
     */
    public static function isWorkingDay($date)
    {
        if (self::isWeekend($date)) {
            return false;
        }

        if (self::isHoliday($date)) {
            return false;
        }

        return true;
    }

    /**
     * 是否周末
     * @param $date
     * @return bool
     */
    public static function isWeekend($date)
    {

        return in_array(self::dataFormat($date,'w'),[6,0]);
    }

    /**
     * 是否节假日
     * @param $date
     * @param bool $currentYear
     * @return bool
     */
    public static function isHoliday($date,$currentYear = true)
    {
        $holiday = $currentYear ? Holiday::where('year','=',self::dataFormat($date,'Y'))->column('id','holiday_date') :  Holiday::column('id','holiday_date');
        return empty($holiday[self::dataFormat($date,'Y-m-d')]) ? false : true;
    }

    /**
     * 结束时间格式化
     * @param $date
     * @param string $format
     * @return string
     */
    public static function dateFormatOfEndTime($date, $format = 'Y-m-d')
    {
        $date = is_numeric($date) ? $date : strtotime($date);

        return date($format, $date) . ' 23:59:59';
    }

    /**
     * 获取多少天前的日期
     * @param $date
     * @param $days
     * @param string $format
     * @return false|float|int|string
     */
    public static function getBeforeDate($date, $days, $format = '')
    {
        $date = is_numeric($date) ? $date : strtotime($date);

        $date = $date - $days * 3600 * 24;

        return $format ? date($format, $date) : $date;
    }
}

看完上述内容,你们对php中怎么实现一个时间助手类有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Java中怎么实现一个时间转换工具类
  2. PHP中如何实现路由类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:PHP中怎么判断用户是否关注该公众号

下一篇:php中怎么实现redis分布式锁

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》