将 date() 转换为正式的更简单的方法?

Easier way to convert date() to formal?

有没有更简单的编码方法? 1.6kb 来调用一个简单的转换似乎过多,因为我的服务器有时每秒单独调用这个转换高达 300 次,或者我只是太拘谨了?

另外请注意,这不是我像这样编写的唯一转换函数,而且服务器构建速度相当快 (~0.3%/24hr)。

如有任何帮助,我们将不胜感激!先感谢您! :)

<?php
function date2formal($a) {
    $y = substr($a, 0, 4);
    switch(substr($a, 5, 2);) {
        case "01": $m = "January"; break;
        case "02": $m = "February"; break;
        case "03": $m = "March"; break;
        case "04": $m = "April"; break;
        case "05": $m = "May"; break;
        case "06": $m = "June"; break;
        case "07": $m = "July"; break;
        case "08": $m = "August"; break;
        case "09": $m = "September"; break;
        case "10": $m = "October"; break;
        case "11": $m = "November"; break;
        case "12": $m = "December"; break;
    }
    switch(substr($a, 8, 2)) {
        case "01": $d = "1st"; break;
        case "02": $d = "2nd"; break;
        case "03": $d = "3rd"; break;
        case "04": $d = "4th"; break;
        case "05": $d = "5th"; break;
        case "06": $d = "6th"; break;
        case "07": $d = "7th"; break;
        case "08": $d = "8th"; break;
        case "09": $d = "9th"; break;
        case "10": $d = "10th"; break;
        case "11": $d = "11th"; break;
        case "12": $d = "12th"; break;
        case "13": $d = "13th"; break;
        case "14": $d = "14th"; break;
        case "15": $d = "15th"; break;
        case "16": $d = "16th"; break;
        case "17": $d = "17th"; break;
        case "18": $d = "18th"; break;
        case "19": $d = "19th"; break;
        case "20": $d = "20th"; break;
        case "21": $d = "21st"; break;
        case "22": $d = "22nd"; break;
        case "23": $d = "23rd"; break;
        case "24": $d = "24th"; break;
        case "25": $d = "25th"; break;
        case "26": $d = "26th"; break;
        case "27": $d = "27th"; break;
        case "28": $d = "28th"; break;
        case "29": $d = "29th"; break;
        case "30": $d = "30th"; break;
        case "31": $d = "31st"; break;
    }
    return $m.", ".$d.", ".$y;
}
?>

谢谢!!!

我稍微调整了您的答案以节省几个字节,并且在 15 分钟的实时测试中,与我的原始代码相比,结果显示 ~0.113%/client! (总的来说,所有功能都会节省很多......估计> 2%!)

function date2formal($a) {$b = new DateTime(substr($a, 0, 10)); return $b->format('F, jS, Y');}

使用日期时间的示例:

$dateStr = '2021-07-20';
$date = new DateTime($dateStr);
$dateFormated = $date->format('F, jS, Y');