php常用日期相关函数

/**
 * 获取指定日期所在周的起止时间
 * @param null $curTime
 * @return array
 */
public function getCurWeek($curTime=null) {
    $curTime = $curTime ? : date('Y-m-d');
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $week = date('w',strtotime($curTime));
    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
    $mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
    $start = date('Y-m-d',strtotime($mon));
    $end = date('Y-m-d',strtotime($start.' + 6 day'));
    return array('start' => $start,'end' => $end);
}
/**
 * 获取指定日期前一周的起止时间
 * @param null $curTime
 * @return array
 */
public function getPrevWeek($curTime=null) {
    $curTime = $curTime ? : time();
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $week = date('w',strtotime($curTime));
    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
    $mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
    $start = date('Y-m-d',strtotime($mon.' - 7 day'));
    print_r($start);
    $end = date('Y-m-d',strtotime($start.' + 6 day'));
    print_r($end);
    return array('start' => $start,'end' => $end);
}
/**
 * 获取指定日期后一周的起止时间
 * @param null $curTime
 * @return array
 */
public function getNextWeek($curTime=null) {
    $curTime = $curTime ? : time();
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $week = date('w',strtotime($curTime));
    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
    $mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
    $start = date('Y-m-d',strtotime($mon.' + 7 day'));
    $end = date('Y-m-d',strtotime($start.' + 6 day'));
    return array('start' => $start,'end' => $end);
}
/**
 * 获取指定日期所在周的连续时间
 * @param null $curTime
 * @return array
 */
public function getWeeksByDate($curTime=null) {
    $weeks = array('周日','周一','周二','周三','周四','周五','周六');
    $weekRange = array();
    $curTime = $curTime ? : date('Y-m-d');
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $week = date('w',strtotime($curTime));
    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
    $mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
    $start = date('Y-m-d',strtotime($mon));
    $end = date('Y-m-d',strtotime($start.' + 6 day'));
    $days = floor((strtotime($end)-strtotime($start))/86400);
    for ($i=0;$i<=$days;$i++) {
        $day = date('Y-m-d',strtotime($start.' '.$i.' day'));
        $weekRange[] = array(
            'day' => date('m-d',strtotime($day)),
            'week' => $weeks[date('w',strtotime($day))],
            'fullDay' => $day
        );
    }
    return $weekRange;
}
/**
 * 获取指定日期所在月的起止时间
 * @param null $curTime
 * @return array
 */
public function getCurMonth($curTime=null){
    $curTime = $curTime ? : date('Y-m-d');
    $timestamp = strtotime($curTime);
    $days = date('t', $timestamp);
    $start = date('Y-m-01', $timestamp);
    $end = date('Y-m-'.$days, $timestamp);
    return array('start' => $start,'end' => $end);
}
/**
 * 获取指定日期所在月的连续时间
 * @param null $curTime
 * @return array
 */
public function getDaysByDate($curTime=null){
    $dayRange = array();
    $curTime = $curTime ? : date('Y-m-d');
    $timestamp = strtotime($curTime);
    $days = date('t', $timestamp);
    $start = date('Y-m-01', $timestamp);
    for ($i=0;$i<$days;$i++) {
        $day = date('Y-m-d',strtotime($start.' '.$i.' day'));
        $dayRange[] = array(
            'day' => date('m-d',strtotime($day)),
            'fullDay' => $day
        );
    }
    return $dayRange;
}
/**
 * 获取指定时间段内的连续时间
 * @param $start 开始时间
 * @param $end 截止时间
 * @param bool $week 是否包含周信息,默认 false
 * @return array
 */
public function getDaysByRange($start,$end,$week=false) {
    $dayRange = array();
    $weeks = array('周日','周一','周二','周三','周四','周五','周六');
    $days = floor((strtotime($end)-strtotime($start))/86400);
    for ($i=0;$i<=$days;$i++) {
        $day = date('Y-m-d',strtotime($start.' '.$i.' day'));
        $dayRange[] = array(
            'day' => date('m-d',strtotime($day)),
            'fullDay' => $day
        );
    }
    if ($week) {
        foreach ($dayRange as $key => $d) {
            $dayRange[$key]['week'] = $weeks[date('w',strtotime($d['fullDay']))];
        }
    }
    return $dayRange;
}
/**
 * 获取指定年份中每个季度的起止时间
 * @param null $year 年份
 * @param int $quarter 季度序号,0 所有季度 | 1 一季度 | 2 二季度 | 3 三季度 | 4 四季度
 * @return mixed
 */
public function getQuarter($year=null,$quarter=0){
    $quarters = array(
        array('start' => '01-01','end' => '12-31'),
        array('start' => '01-01','end' => '03-31'),
        array('start' => '04-01','end' => '06-30'),
        array('start' => '07-01','end' => '09-30'),
        array('start' => '10-01','end' => '12-31')
    );
    $year = $year ? : date('Y');
    $fullQuarter = $quarters[$quarter];
    $fullQuarter['start'] = sprintf('%s-%s',$year,$fullQuarter['start']);
    $fullQuarter['end'] = sprintf('%s-%s',$year,$fullQuarter['end']);
    return $fullQuarter;
}
/**
 * 获取指定日期对应的时间戳,默认为当前日期
 * @param string $dateTime 时间,格式示例:Y-m-d H:i:s
 * @return int
 */
public function getTimeStamp($dateTime=''){
    $date = new \DateTime($dateTime);
    return $date->format('U');
}
/**
 * 格式化时间戳
 * @param $time 时间戳
 * @param $format 时间格式,如:Y-m-d H:i:s
 * @return string
 */
public function formatTimeStamp($time,$format=null){
    $format = $format ? : 'Y-m-d H:i:s';
    $date = new \DateTime(sprintf('@%s',$time));
    //设置中国时区
    $date->setTimezone(new \DateTimeZone('PRC'));
    return $date->format($format);
}
/**
 * 获取当前时间戳,精确到毫秒
 * @return number
 */
public function getMicroTime(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
/**
 * 格式化时间戳,精确到毫秒,x代表毫秒
 * @param $time 时间戳
 * @param $format 时间格式,如:Y-m-d H:i:s
 * @return mixed
 */
public function formatMicroTime($time,$format=null) {
    if (strpos($time, '.') !== false) {
        list($usec, $sec) = explode(".", $time);
    }else{
        $usec = substr($time,0,10);
        $sec = str_replace($usec, '', $time);
    }
    $format = $format ? $format.'.x' : 'Y-m-d H:i:s.x';
    $date = date($format,$usec);
    return str_replace('x', $sec, $date);
}
/**
 * 获取时间差
 * @param $dateTime1 格式,如:Y-m-d H:i:s
 * @param $dateTime2 格式,如:Y-m-d H:i:s
 * @param $type 时间差类型,H 小时 | m 分钟 | s 秒,默认为天
 * @return mixed
 */
public function getTimeDiff($dateTime1,$dateTime2,$type=null){
    $diff = null;
    $date1 = new \DateTime($dateTime1);
    $date2 = new \DateTime($dateTime2);
    if ($type) {
        $seconds1 = $date1->format('U');
        $seconds2 = $date2->format('U');
        $temp = abs($seconds1 - $seconds2);
        $type = strtoupper($type);
        switch ($type) {
            case 'H':
                $diff = floor($temp/3600);
                break;
            case 'M':
                $diff = floor($temp/60);
                break;
            case 'S':
                $diff = $temp;
                break;
            default:
                break;
        }
    } else {
        $timeDiff = $date1->diff($date2);
        $diff = $timeDiff->days;
    }
    return $diff;
}
/**
 * 时间偏移量增量计算
 * @param $intervalArr 偏移量数组,格式:array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
 * @param string $dateTime 时间,格式示例:Y-m-d H:i:s
 * @return string
 */
public function addDateTime($intervalArr,$dateTime=''){
    $date = new \DateTime($dateTime);
    $interval = sprintf('P%sY%sM%sDT%sH%sM%sS',$intervalArr['year'],$intervalArr['mon'],
        $intervalArr['day'],$intervalArr['hour'],$intervalArr['min'],$intervalArr['sec']);
    $date->add(new \DateInterval($interval));
    return $date->format('U');
}
/**
 * 时间偏移量减量计算
 * @param $intervalArr 偏移量数组,格式:array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
 * @param string $dateTime 时间,格式示例:Y-m-d H:i:s
 * @return string
 */
public function subDateTime($intervalArr,$dateTime=''){
    $date = new \DateTime($dateTime);
    $interval = sprintf('P%sY%sM%sDT%sH%sM%sS',$intervalArr['year'],$intervalArr['mon'],
        $intervalArr['day'],$intervalArr['hour'],$intervalArr['min'],$intervalArr['sec']);
    $date->sub(new \DateInterval($interval));
    return $date->format('U');
}
/**
 * 比较日期大小
 * @param $dateTime1 格式,如:Y-m-d H:i:s
 * @param $dateTime2 格式,如:Y-m-d H:i:s
 * @return int 参数1>参数2时,返回1,参数1<参数2时,返回-1,否则返回0
 */
public function compareDate($dateTime1,$dateTime2){
    $date1 = new \DateTime($dateTime1);
    $date2 = new \DateTime($dateTime2);
    $seconds1 = $date1->format('U');
    $seconds2 = $date2->format('U');
    if ($seconds1 > $seconds2) {
        return 1;
    }
    if ($seconds1 < $seconds2) {
        return -1;
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值