时间工具

<?php
/**
 * User: yuzhao
 * CreateTime: 2018/10/30 上午11:58
 * Description: 时间工具类
 */

class TimeTool {

    /**
     * User: yuzhao
     * CreateTime: 2018/11/14 上午11:20
     * @var
     * Description: 统一时间
     */
    private $time=0;

    /**
     * User: yuzhao
     * CreateTime: 2018/11/14 上午11:25
     * @var
     * Description:  当前对象
     */
    private static $myself;

    /**
     * TimeTool constructor.
     */
    public function __construct($time)
    {
        $this->time = $time;
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/14 上午11:20
     * Description: 单例模式返回当前对象
     */
    public static function instance($time=null) {
        if (self::$myself === null || (self::$myself->time !== $time && $time!==null)) {
            self::$myself = new TimeTool($time);
        }
        return self::$myself;
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/10/30 下午2:42
     * @return array
     * Description: 获取时间基本信息
     */
    public function getTimeBaseInfo() {
        return array(
            // 当前时间
            'now_time'  => $this->time,
            'ymd'       => date('Ymd', $this->time),
            'today_week'=> $this->getTodayWeek()
        );
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/14 上午10:31
     * @param $day
     * @param string $format
     * @return mixed
     * Description: 获取本周日期
     */
    public function getNowWeekDay($day, $format = "Ymd") {
        $day--;
        $week = date('w', $this->time);
        //星期日排到末位
        if(empty($week)){
            $week=7;
        }
        return date($format,strtotime( '+' . $day+1-$week .' days',$this->time));
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/7 上午10:51
     * @return false|int
     * Description: 获取上一周的某一天
     */
    public function getLastWeekDay($day,$format='Ymd') {
        $day--;
        $week = date('w', $this->time);
        //星期日排到末位
        if(empty($week)){
            $week=7;
        }
        return date($format,strtotime( '+' . $day+1-$week-7 .' days',$this->time));
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/13 下午11:37
     * Description: 后去今天是星期几
     */
    public function getTodayWeek() {
        return date("w", $this->time);
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/7 上午10:52
     * Description: 获取今天的开始时间
     */
    public function getTodayStart() {
        return mktime(0,0,0,date('m'),date('d'),date('Y'));
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/7 上午10:52
     * Description: 获取今天的结束时间
     */
    public static function getTodayEnd() {
        return mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/7 上午10:55
     * @return false|string
     * Description: 获取任务名
     */
    public static function getNowWeekStartTime() {
       return date('Ymd', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
    }

    /**
     * User: yuzhao
     * CreateTime: 2018/11/14 上午11:44
     * @param $day1
     * @param $day2
     * @return float|int
     * Description: 获取两个日期相差的天数
     */
    public static function diffBetweenTwoDays ($day1, $day2) {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值