/*
 * 一组数组中获取最大时间戳
 * @param string $images_ids
 */
function get_max_time_list($list = [])
{
    if (is_array($list)) {
        $len = count($list);
        for ($i = 0; $i < $len - 1; $i++) {//循环对比的轮数
            for ($j = 0; $j < $len - $i - 1; $j++) {//当前轮相邻元素循环对比
                if ($list[$j]['sign_end_time'] > $list[$j + 1]['sign_end_time']) {//如果前边的大于后边的
                    $tmp = $list[$j]['sign_end_time'];//交换数据
                    $list[$j]['sign_end_time'] = $list[$j + 1]['sign_end_time'];
                    $list[$j + 1]['sign_end_time'] = $tmp;
                }
            }
        }
        $max_time = $list[0]['sign_end_time'];
        return !empty($max_time) ? $max_time : '';
    }
}
/**
 * @desc 给定日期获取7天日期时间
 * @param $date Y-m-d
 * @return array
 */
function nearest_days($date)
{
    $date_1 = date('Y-m-d');
    $date_2 = date('Y-m-d', strtotime($date));
    $d1 = strtotime($date_1);
    $d2 = strtotime($date_2);
    $days = round(($d1 - $d2) / 3600 / 24);
    $dates_1 = [];
    $dates_2 = [];
    $nums_1 = 0;
    $nums_2 = 3;
    if ($days >= 3) {
        $nums_1 = 3;
    } else {
        $nums_1 = $days;
        $nums_2 = 7 - $days - 1;
    }
    for ($i = $nums_1; $i > 0; $i--) {
        $date_3 = strtotime($date) + (3600 * 24 * $i);
        $dates_1[] = date('Y-m-d', $date_3);
    }
    for ($j = 1; $j <= $nums_2; $j++) {
        $date_4 = strtotime($date) - (3600 * 24 * $j);
        $dates_2[] = date('Y-m-d', $date_4);
    }
    $data = array_merge($dates_1, [$date], $dates_2);
    $data_list = [];
    for ($i = 6; $i >= 0; $i--) {
        $data_list[] = $data[$i];
    }
    return $data_list;
}
/**
 * @desc 给定日期获取7个月份日期时间【当前时间在中间】
 * @param $date Y-m
 * @return array
 */
function nearest_mouth($date)
{
    //$data = ['2021-1','2020-12','2020-11','2020-10','2020-9','2020-8','2020-7'];
    $now_year = date('Y');
    $now_month = date('m');
    if (!empty($date)) {
        $d = explode('-', $date);
        if ($d[0] > $now_year || ($d[0] == $now_year) && ($d[1] > $now_month)) {
            $date = date('Y-m');
        }
    }
    $year = date('Y', strtotime($date));
    $month = date('m', strtotime($date));
    $up = 3;
    $down = 3;
    $total_month = 12;
    $total_num = 7;
    $data_up = [];
    $data_down = [];
    if ($year == $now_year) {
        if (($month + 3) >= $now_month) {
            $up = $now_month - $month;
        } elseif (($month + 3) <= $now_month) {
            $up = 3;
        }
        for ($i = $up; $i > 0; $i--) {
            $data_up[] = date('Y-m', strtotime($now_year . '-' . ($month + $i)));
        }
        $down = $total_num + $up;
        $j = 1;
        $s = 0;
        for ($i = $total_month + $up; $i >= $down; $i--) {
            if ($month - $j > 0) {
                $data_down[] = date('Y-m', strtotime(($now_year) . '-' . ($month - $j)));
                $j++;
            } else {
                $data_down[] = date('Y-m', strtotime(($now_year - 1) . '-' . (12 - $s)));
                $s++;
            }
        }
    } else {
        if ($month == 9) {
            for ($i = 3; $i > 0; $i--) {
                $data_up[] = date('Y-m', strtotime($year . '-' . ($month + $i)));
            }
            for ($j = 1; $j <= 3; $j++) {
                $data_down[] = date('Y-m', strtotime($year . '-' . ($month - $j)));
            }
        } elseif ($month > 9) {//跨到下一年
            $nums = 3 - (12 - $month);
            $n = $nums;
            if (($year + 1) == $now_year) {
                if ($nums > $now_month) {
                    $n = $now_month;
                }
            }
            for ($i = $n; $i > 0; $i--) {
                $data_up[] = date('Y-m', strtotime(($year + 1) . '-' . ($i)));
            }
            if (($year + 1) < $now_year) {
                $j = 3 - count($data_up);
                for ($i = $j; $i > 0; $i--) {
                    $data_up[] = date('Y-m', strtotime(($year) . '-' . ($month + $i)));
                }
            }
            $s = 7 - count($data_up) - 1;
            for ($i = 1; $i <= $s; $i++) {
                $data_down[] = date('Y-m', strtotime(($year) . '-' . ($month - $i)));
            }
        } elseif ($month < 4) {
            for ($i = 3; $i > 0; $i--) {
                $data_up[] = date('Y-m', strtotime($year . '-' . ($month + $i)));
            }
            for ($s = 1; $s < $month; $s++) {
                $data_down[] = date('Y-m', strtotime(($year) . '-' . ($month - $s)));
            }
            $n = 3 - count($data_down);
            for ($i = 0; $i < $n; $i++) {
                $data_down[] = date('Y-m', strtotime(($year - 1) . '-' . (12 - $i)));
            }
        } else {
            //($month<9&&$month>=4){
            for ($i = 3; $i > 0; $i--) {
                $data_up[] = date('Y-m', strtotime($year . '-' . ($month + $i)));
            }
            for ($j = 1; $j <= 3; $j++) {
                $data_down[] = date('Y-m', strtotime($year . '-' . ($month - $j)));
            }
        }
    }
    $data = array_merge($data_up, [$date], $data_down);
    $data_list = [];
    for ($ie = 6; $ie >= 0; $ie--) {
        $data_list[] = $data[$ie];
    }
    return $data_list;
}
/**
 * @desc 给定日期获取7个年份时间
 * @param $date Y
 * @return array
 */
function nearest_year($date)
{
    $year_1 = date('Y');
    $year_2 = $date;
    $days = $year_1 - $year_2;
    $dates_1 = [];
    $dates_2 = [];
    $nums_1 = 0;
    $nums_2 = 3;
    if ($days >= 3) {
        $nums_1 = 3;
    } else {
        $nums_1 = $days;
        $nums_2 = 7 - $days - 1;
    }
    for ($i = $nums_1; $i > 0; $i--) {
        $dates_1[] = $date + $i;
    }
    for ($j = 1; $j <= $nums_2; $j++) {
        $dates_2[] = $date - $j;
    }
    $data = array_merge($dates_1, [$date], $dates_2);
    $data_list = [];
    for ($i = 6; $i >= 0; $i--) {
        $data_list[] = $data[$i];
    }
    return $data_list;
}
/**
 * 获取目录下文件和目录
 * @path 路径
 * @return array
 */
function getDirContent($path)
{
    if (!is_dir($path)) {
        return false;
    }
    //readdir方法
    // $dir = opendir($path);
    // $arr = array();
    // while ($content = readdir($dir)) {
    //     if ($content != '.' && $content != '..') {
    //         $arr[] = $content;
    //     }
    // }
    // closedir($dir);
    //scandir方法
    $arr = array();
    $data = scandir($path);
    foreach ($data as $value) {
        if ($value != '.' && $value != '..') {
            $arr[] = $value;
        }
    }
    return $arr;
}
/**
 * 模糊搜索数组
 * @param array $data 数组
 * @param string $key 匹配字符串
 * @param string $info 字段
 * @return array
 */
function arrayLikes($data=[],$key=''){
    foreach($data as $v ){
        // print_r($v);exit;
        if (strstr($v,$key) !== false ){
				    // 自己修改要比对的内容
            return ['upgrade_url'=>base_url().'/version/'.$v,'upgrade_version'=>self::extract_number($v)];
        }
    }
    return ['upgrade_url'=>'','upgrade_version'=>''];
}
/**
 * 文本中提取数字
 * @param string $str 待提取字符串
 * @return mixed|string 返回数字
 */
function extract_number($str=''){
    $str=trim($str);
    if(empty($str)){return '';}
    $reg='/(\d(\.\d+)?)/is';//匹配数字的正则表达式
    preg_match_all($reg,$str,$result);
    // print_r($result);exit;
    if(is_array($result)&&!empty($result)&&!empty($result[0])){
        return implode('.',$result[0]);
    }
    return '';
}
if (!function_exists('delete_dir')) {
    /**
     * 遍历删除文件夹所有内容
     * @param  string $dir 要删除的文件夹
     */
    function delete_dir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != '.' && $file != '..') {
                $filepath = $dir . DIRECTORY_SEPARATOR . $file;
                if (is_dir($filepath)) {
                    delete_dir($filepath);
                } else {
                    @unlink($filepath);
                }
            }
        }
        closedir($dh);
        @rmdir($dir);
    }
}