当前位置:网站首页>PHP common date related functions

PHP common date related functions

2022-06-09 16:27:00 Uncle Muyu

/**
 *  Get the start and end time of the week of the specified date 
 * @param null $curTime
 * @return array
 */
public function getCurWeek($curTime=null) {
    $curTime = $curTime ? : date('Y-m-d');
    // Gets the day ordinal of the current week   Sunday is  0  Monday to Saturday is  1 - 6
    $week = date('w',strtotime($curTime));
    // Get the start date of the week , If $w yes 0, It means Sunday , subtract  6  God 
    $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);
}
/**
 *  Get the start and end time of the week before the specified date 
 * @param null $curTime
 * @return array
 */
public function getPrevWeek($curTime=null) {
    $curTime = $curTime ? : time();
    // Gets the day ordinal of the current week   Sunday is  0  Monday to Saturday is  1 - 6
    $week = date('w',strtotime($curTime));
    // Get the start date of the week , If $w yes 0, It means Sunday , subtract  6  God 
    $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);
}
/**
 *  Get the start and end time of one week after the specified date 
 * @param null $curTime
 * @return array
 */
public function getNextWeek($curTime=null) {
    $curTime = $curTime ? : time();
    // Gets the day ordinal of the current week   Sunday is  0  Monday to Saturday is  1 - 6
    $week = date('w',strtotime($curTime));
    // Get the start date of the week , If $w yes 0, It means Sunday , subtract  6  God 
    $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);
}
/**
 *  Gets the continuous time of the week of the specified date 
 * @param null $curTime
 * @return array
 */
public function getWeeksByDate($curTime=null) {
    $weeks = array(' Sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday ');
    $weekRange = array();
    $curTime = $curTime ? : date('Y-m-d');
    // Gets the day ordinal of the current week   Sunday is  0  Monday to Saturday is  1 - 6
    $week = date('w',strtotime($curTime));
    // Get the start date of the week , If $w yes 0, It means Sunday , subtract  6  God 
    $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;
}
/**
 *  Get the start and end time of the month of the specified date 
 * @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);
}
/**
 *  Gets the continuous time of the month of the specified date 
 * @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;
}
/**
 *  Gets the continuous time in the specified time period 
 * @param $start  Starting time 
 * @param $end  By the time 
 * @param bool $week  Whether to include weekly information , Default  false
 * @return array
 */
public function getDaysByRange($start,$end,$week=false) {
    $dayRange = array();
    $weeks = array(' Sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday ');
    $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;
}
/**
 *  Get the start and end time of each quarter in the specified year 
 * @param null $year  year 
 * @param int $quarter  Quarter serial number ,0  All quarters  | 1  First quarter  | 2  The two quarter  | 3  third quater  | 4  In the fourth quarter 
 * @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;
}
/**
 *  Get the timestamp corresponding to the specified date , Default to current date 
 * @param string $dateTime  Time , Format example :Y-m-d H:i:s
 * @return int
 */
public function getTimeStamp($dateTime=''){
    $date = new \DateTime($dateTime);
    return $date->format('U');
}
/**
 *  Format timestamps 
 * @param $time  Time stamp 
 * @param $format  Time format , Such as :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));
    // Set China time zone 
    $date->setTimezone(new \DateTimeZone('PRC'));
    return $date->format($format);
}
/**
 *  Get the current timestamp , Accurate to milliseconds 
 * @return number
 */
public function getMicroTime(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
/**
 *  Format timestamps , Accurate to milliseconds ,x On behalf of ms 
 * @param $time  Time stamp 
 * @param $format  Time format , Such as :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);
}
/**
 *  Get time difference 
 * @param $dateTime1  Format , Such as :Y-m-d H:i:s
 * @param $dateTime2  Format , Such as :Y-m-d H:i:s
 * @param $type  Time difference type ,H  Hours  | m  minute  | s  second , Default to days 
 * @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;
}
/**
 *  Time offset increment calculation 
 * @param $intervalArr  Offset array , Format :array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
 * @param string $dateTime  Time , Format example :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');
}
/**
 *  Time offset decrement calculation 
 * @param $intervalArr  Offset array , Format :array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
 * @param string $dateTime  Time , Format example :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');
}
/**
 *  Compare date size 
 * @param $dateTime1  Format , Such as :Y-m-d H:i:s
 * @param $dateTime2  Format , Such as :Y-m-d H:i:s
 * @return int  Parameters 1> Parameters 2 when , return 1, Parameters 1< Parameters 2 when , return -1, Otherwise return to 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;
}




原网站

版权声明
本文为[Uncle Muyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091557248164.html