<?php
/**
* User: yuzhao
* CreateTime: 2018/10/30 上午11:58
* Email: shixi_yuzhao@staff.weibo.com
* Description: 时间工具类
*/
class TimeTool {
/**
* User: yuzhao
* CreateTime: 2018/11/14 上午11:20
* @var
* Email: shixi_yuzhao@staff.weibo.com
* Description: 统一时间
*/
private $time=0;
/**
* User: yuzhao
* CreateTime: 2018/11/14 上午11:25
* @var
* Email: shixi_yuzhao@staff.weibo.com
* Description: 当前对象
*/
private static $myself;
/**
* TimeTool constructor.
*/
public function __construct($time)
{
$this->time = $time;
}
/**
* User: yuzhao
* CreateTime: 2018/11/14 上午11:20
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* Description: 后去今天是星期几
*/
public function getTodayWeek() {
return date("w", $this->time);
}
/**
* User: yuzhao
* CreateTime: 2018/11/7 上午10:52
* Email: shixi_yuzhao@staff.weibo.com
* Description: 获取今天的开始时间
*/
public function getTodayStart() {
return mktime(0,0,0,date('m'),date('d'),date('Y'));
}
/**
* User: yuzhao
* CreateTime: 2018/11/7 上午10:52
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* 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
* Email: shixi_yuzhao@staff.weibo.com
* 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;
}
}
网友评论