zoukankan      html  css  js  c++  java
  • 解析时间parse time

    下面是一个解析时间的一个类

    <?php
    /**
     * @purpose : 解析时间
     * author: 袋鼠
     * date: 2019/3/1
     * time: 19:43
     */
    
    class ParseTime
    {
        /**
         * @purpose: 将一个时间范围解析成datetime格式或者timestamp格式
         * @param string $value 由分隔符(如:-)分割的一个时间范围,格式:Y/m/d H:i:s - Y/m/d H:i:s,如2019-03-02 12:00:00 -2019-03-02 14:00:00
         * @param string $type 返回时间类型,支持timestamp和datetime两种类型
         * @param string $step 分隔符,默认为 -
         * @param bool $autocomplate 是否自动补全时分秒,默认为true
         * @return array 相应时间格式的数组,如:array(2) { [0]=> string(19) "2019/03/02 12:00:00" [1]=> string(19) "2019/03/02 14:59:59" }
         */
        public static function parseDateRange($value,$type,$step='-',$autocomplate=true)
        {
            if(empty($value)){
                return false;
            }
    
            $ts = array_map(function($v){
                return trim($v);
            },explode($step,$value));
    
            //是否自动补全H:i:s分开处理
            if($autocomplate && count($ts) == 2) {
                //时间戳格式和datetime格式的分别进行转换为时间戳格式
                if (is_numeric($ts[1]) && date('His', $ts[1]) === '000000') {
                    $ts[1] = $ts[1] + 86399; // 加上 23:59:59
                }elseif(date('His',strtotime($ts[1])) == '00000'){
                    $ts[0] = strtotime($ts[0]);
                    $ts[1] = strtotime($ts[1]) + 86399;
                }
            }elseif(count($ts) == 2 && $ts[0] == $ts[1]){
                if (is_numeric($ts[1]) && date('His', $ts[1]) === '000000') {
                    $ts[1] = $ts[1] + 86399;
                }elseif(date('His',strtotime($ts[1])) == '00000'){
                    $ts[0] = strtotime($ts[0]);
                    $ts[1] = strtotime($ts[1]) + 86399;
                }
            }
    
            if($type == 'timestamp'){
                return $ts;
            }else{
                return array_map(function($v){
                    return date('Y-m-d H:i:s',$v);
                },$ts);
            }
    
        }
    
        /**
         * @purpose: 将H:i:s格式的时间范围转换成整数值
         * @param string $value 时间范围,格式为 H:i:s - H:i:s, 例如12:00:00 - 14:59:59
         * @param string $step 分割符,默认为 -
         * @return array 转换后的整数值,如:array(2) { [0]=> int(43200) [1]=> int(53999) }
         */
        public static function parseHourRange($value,$step='-')
        {
            if(empty($value)){
                return false;
            }
    
            return array_map(function ($v){
                //如果没有:,返回整数值,注意此处要用 === 而不是 ==,因为 0==false
                if(strpos($v,':') === false){
                    return intval($v);
                }
                $arrTime = array_reverse(explode(':',$v));
                $i = $s = 0;
                //将倒序后的时间乘以60的$i次方,比如14::59:59=>倒序[59,59,14]=>整数值:59*60^0(秒) + 59*60^1(分)+ 14*60^2(时)=53999
                foreach($arrTime as $time){
                    $s += $time * pow(60,$i) ;
                    $i ++;
                }
                return $s;
            },explode($step,$value));
        }
    }

    此类为袋鼠工作中所总结,如有转载,请注明出处:https://www.cnblogs.com/chrdai/p/10461522.html

  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/chrdai/p/10461522.html
Copyright © 2011-2022 走看看