php 常用函数

php 常用函数

//文件大小输出
function formatFileSize($size)
    {
        $units = array('B', 'KB', 'MB', 'GB');
        $unit = 0;

        while ($size > 1024) {
            $size /= 1024;
            $unit++;
        }

        return round($size, 2) . '' . $units[$unit];
    }
//输出某日星期几
function week($date): string
    {
        $timestamp = strtotime($date); // 设置特定日期
        $day_of_week = date('N', $timestamp); // 获取星期几的数值,1 表示星期一,7 表示星期日
        $week_days = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');
        // 获取对应的星期几全名
        return $week_days[$day_of_week - 1];
    }
//获取范围内的日期
function getBetweenDates(string $start_at, string $end_at): array
    {
        $format = 'Y-m-d';
        $current = \DateTime::createFromFormat($format, $start_at);
        $endDate = \DateTime::createFromFormat($format, $end_at);
        $dates = [];
        while ($current <= $endDate) {
            //echo $current->format($format) . "\n";
            $date = $current->format($format);
            $dates[] = $date;
            $current->modify('+1 day');
        }
        return $dates;
    }

留下回复