<?php
/**
* @todo 时间工具类
* @author jixiaolong
*/
class TimeTool{
private $week_map;
const ONE_SECOND = 1;
const ONE_MINUTE = 60;
const ONE_HOUR = 3600;
const ONE_DAY =86400; //一天的秒钟数24*60*60
const TIME_STR = 1; //字符串`
const TIME_STAMP = 2; //时间戳
function __construct(){
$this->doWeekMap();
}
/*
* 初始化week_map
*/
private function doWeekMap(){
$this->week_map = array(
1=>'星期一',
2=>'星期二',
3=>'星期三',
4=>'星期四',
5=>'星期五',
6=>'星期六',
7=>'星期日',
);
}
/*
* 获取某天所在的那一周的日期
* @param string $date 日期
* @return array $days 日期
*/
public function getWeekDays($date){
$days = array();
$timestamp = strtotime($date);
$w = date('w',$timestamp); //周几
$w = $w==0 ? 7 : $w;
$mondayTime = $timestamp-($w-1)*TimeFunc::ONE_DAY; //该周周一的时间戳
$n = 1;
$time = $mondayTime;
do{
$cur_date = date('Y-m-d',$time);
$days[$n] = array(
'date'=>$cur_date, //日期
'n'=>$n, //星期几(1-7)
'nweek'=>$this->week_map[$n], //星期几
);
$time += TimeFunc::ONE_DAY;
$n++;
}
while ($n<8);
return $days;
}
/**
* @todo 获取指定时间点之后N年N月N天N小时之后的时间
* @param string $time
* @param int $years
* @param int $months
* @param int $days
* @param int $hours
* @param int $minutes
* @param int $seconde
*/
public function getTimeAfterTime($time,$years=0,$months=0,$days=0,$hours=0,$minutes=0,$secondes=0){
$time = $this->changeToStamp($time);
if ($time===false){
return false;
}
//计算应该返回的时间戳
$time += $days*TimeFunc::ONE_DAY+$hours*TimeFunc::ONE_HOUR+$minutes*TimeFunc::ONE_MINUTE+$secondes*TimeFunc::ONE_SECOND;
if ($years!=0 || $months!=0){
$year = date('Y',$time);
$month = date('n',$time);
$day = date('j',$time);
$hour = date('G',$time);
$minute = date('i',$time);
$seconde = date('s',$time);
$m = $month+$months;
if ($m>=12){
$month = $m%12;
$month = $month==0 ? 12 : $month;
$year += intval($m/12);
}
else{
$month += $m;
}
$year += $years;
$time = mktime($hour,$minute,$seconde,$month,$day,$year);
}
return $time;
}
/**
* @todo 获取整点或半点时间
* @param int/string $time 时间/时间戳
* @return int 整点或半点时间(时间戳)
*/
public function getHalfHourTime($time){
$time = $this->changeToStamp($time);
if($time===false){
return false;
}
$minute = date('i',$time);
$i = intval($minute);
if ($i<=30){
$time += (30-$i)*TimeFunc::ONE_MINUTE;
}
else {
$time += (60-$i)*TimeFunc::ONE_MINUTE;
}
return $time;
}
/**
* @todo 将时间转化为时间戳
* 时间戳变,非时间戳转化为时间戳,失败返回false
* @param int/string $time 字符串时间或时间戳
* @return int $time 时间戳
*/
public function changeToStamp($time){
if (is_numeric($time)===false){
//非时间戳形式的参数转化为时间戳
$time = strtotime($time);
if ($time<0 ||$time===false){
return false;
}
}
return $time;
}
/**
* @todo 比较2个时间的大小
* @param int/string $time1 时间1(字符串或时间戳)
* @param unknown_type $time2 时间2(字符串或时间戳)
* @return 1/-1/0 时间1比2大,返回1,小,返回-1,相等,返回0
*/
public function timeAccurateCompare($time1,$time2){
$time1 = $this->changeToStamp($time1);
$time2 = $this->changeToStamp($time2);
if ($time1>$time2){
return 1;
}
else if ($time1<$time2){
return -1;
}
else if ($time1==$time2){
return 0;
}
}
/**
* @todo 判断是星期几
* Enter description here ...
* @param int $num 星期几(1-7)
*/
public function isWeekN($num,$time){
if ($num<1 || $num>7){
return false;
}
$time_stamp = $this->changeToStamp($time);
if ($time_stamp == false){
return false;
}
$m = date('w',$time_stamp);
return ($m == $num) ? true : false;
}
/**
* @todo 比较2个时间点是否在一个指定时间范围内
* Enter description here ...
* @param string/int $time1 时间1
* @param string/int $time2 时间2
* @param int $range 时间范围(秒)
* @return boolean 是/否
*/
public function compareTimeWithRange($time1,$time2,$range){
$time1 = $this->changeToStamp($time1);
$time2 = $this->changeToStamp($time2);
$now_range = abs($time1-$time2);
if ($now_range<=$range){
return true;
}
return false;
}
/**
* @todo 获取2个时间点的差值
* Enter description here ...
* @param string/int $time1 时间1
* @param string/int $time2 时间2
* @return int 时间差(秒)
*/
public function getTimeDiff($time1,$time2){
$time1 = $this->changeToStamp($time1);
$time2 = $this->changeToStamp($time2);
return abs($time1-$time2);
}
/**
* @todo 获取给定时间的上月的时间戳
* Enter description here ...
* @param int/string $time 给定时间
* @param int time_type 返回时间类型
* @return int 上月1日的时间戳
*/
public function getLastMonth($time,$time_type=self::TIME_STAMP){
$time = $this->changeToStamp($time);
$year = date('Y',$time);
$month = date('m',$time);
$year = intval($year);
$month = intval($month);
if ($month ==1){
$year--;
$month = 12;
}
else {
$month--;
}
$time = mktime(0,0,0,$month,1,$year);
if (self::TIME_STR == $time_type){
return date('Y-m',$time);
}
else{
return $time;
}
}
/**
* 获取当前月份前或者后几个月的月份
* @param int $tag 月份偏移量,正数为向前推,负数向后推
* @param unknown_type $base 基准时间,不传默认为当前时间
* @return 结果月1号的时间戳
*/
//modify extend function to support str type return by jixiaolong 2013/3/16
public function getMonthBefore($tag,$base='',$time_type=self::TIME_STAMP){
if('' == $base){
$month = date('m');
$year = date('Y');
}
else {
$time = $this->changeToStamp($base);
$year = date('Y',$time);
$month = date('m',$time);
}
$new_month = intval($month) - $tag;
$new_year = intval($year);
$time = mktime(0,0,0,$new_month,1,$new_year);
if (self::TIME_STR == $time_type){
$time = date('Y-m',$time);
}
return $time;
}
/**
* @todo 获取时间详情(月,日,年等)
* @param string $time_str 时间字符串
*/
public static function getTimeDetail($time_str){
if (empty($time_str)){
return false;
}
$detail = array();
$pos1 = strpos($time_str, '-' , 0);
$pos2 = strpos($time_str, '-' , 5);
if ($pos1 == 4 && $pos2 == 7){
//标准日期用字符串截取获取
$detail['year'] = substr($time_str, 0, 4);
$detail['month'] = substr($time_str, 5, 2);
$detail['day'] = substr($time_str, 8, 2);
}
else{
//非标准日期,通过转化为时间戳获取
$time = strtotime($time_str);
$detail['year'] = date('Y' ,$time);
$detail['month'] = date('m' ,$time);
$detail['day'] =date('d' ,$time);
}
$detail['year'] = intval($detail['year']);
$detail['month'] = intval($detail['month']);
$detail['day'] = intval($detail['day']);
return $detail;
}
/**
* @todo 获取指定月的最后一天
* Enter description here ...
* @param int $year 年
* @param int $month 月
* @param boolean $return_type 返回类型
*/
public function getLastDay($year,$month,$return_type){
$year = intval($year);
$month = intval($month);
if ($month == 2){
//获取下个月1号
$time = mktime(0,0,0,$month,1,$year);
$last_month = $this->getMonthBefore(-1,$time);
//获取上个月最后1天的时间戳
$return_time = $last_month - self::ONE_DAY;
}
else if (in_array($month, array(1,3,5,7,8,10,12))) {
$return_time = mktime(0,0,0,$month,31,$year);
}
else{
$return_time = mktime(0,0,0,$month,30,$year);
}
if ($return_type == self::TIME_STR){
$return_time = date('Y-m-d',$return_time);
}
return $return_time;
}
public function getFirstDay($year,$month,$return_type){
$year = intval($year);
$month = intval($month);
$time_stamp = mktime(0,0,0,$month,1,$year);
if (self::TIME_STR == $return_type){
return date('Y-m-d',$time_stamp);
}
else{
return $time_stamp;
}
}
/**
* @todo 将秒转变为小时
* @param double $seconds 秒
* @author jixiaolong 2013/3/22
*/
public static function changeToHours($seconds){
return $seconds/self::ONE_HOUR;
}
/**
* @author jixiaolong 2013-7-3上午09:50:39
* @todo 获取相应时间单位的时间
* @param int $stamp_diff 时间戳差
* @param int $unit 时间单位
* @param boolean $is_round 是否要取整
* @throws Exception
* @return
*/
private function getTime($stamp_diff,$unit,$is_round=true){
if (0 >= $unit){
throw new Exception('时间单位不能小于等于零!', 0);
}
$time = $stamp_diff/$unit;
if ($is_round){
$time = floor($time);
}
return $time;
}
/**
* @author jixiaolong 2013-7-3上午09:49:11
* @todo 获取分钟数
* @param int $stamp_diff 时间戳差
* @param boolean $is_round 是否取整
* @return
*/
public function getMinutes($stamp_diff,$is_round){
return $this->getTime($stamp_diff, self::ONE_MINUTE, $is_round);
}
/**
* @author jixiaolong 2013-7-3上午09:54:21
* @todo 将指定时间差转换为小时
* @param int $stamp_diff 时间差
* @param boolean $is_round 是否取整
* @return
*/
public function getHours($stamp_diff, $is_round){
return $this->getTime($stamp_diff, self::ONE_HOUR, $is_round);
}
/**
* @author jixiaolong 2013-7-31下午03:38:48
* @todo 比较时间大小
* @param string/int $time1 时间1
* @param string/int $time2 时间2
* @return int 1大于,0相等,-1小于
*/
public function compareTime($time1,$time2){
$time1 = $this->changeToStamp($time1);
$time2 = $this->changeToStamp($time2);
if ($time1 == $time2){
return 0;
}
else if ($time1 > $time2){
return 1;
}
else{
return -1;
}
}
}
PHP时间工具类
最新推荐文章于 2023-10-20 17:18:08 发布