You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP将出生日期(DOB)转换为规范文字格式

如何将出生日期(DOB)转换为英文文字格式

首先,你的现有代码存在几个明显问题:

  • 你用explode('.', $dobObject)分割日期,但你的输入是斜杠/分隔的,分割符不匹配
  • date()函数只能转换日期的格式字符串,没法直接把数字转换成英文文字表述

下面我给你一套完整的PHP解决方案,通过几个辅助函数来实现数字到英文单词的转换,完美处理日期的日、月、年各部分:

步骤1:实现数字转英文的辅助函数

我们需要两个核心函数:一个把数字转成基数词(用于年份、月份数字),另一个把数字转成序数词(用于日期的日部分,比如21要转成twenty-first)

// 将数字转换为基数词英文
function numberToWords($num) {
    $ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
             'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
             'seventeen', 'eighteen', 'nineteen'];
    $tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

    if ($num < 20) {
        return $ones[$num];
    } elseif ($num < 100) {
        $ten = floor($num / 10);
        $remainder = $num % 10;
        return $tens[$ten] . ($remainder ? '-' . $ones[$remainder] : '');
    } elseif ($num < 1000) {
        $hundred = floor($num / 100);
        $remainder = $num % 100;
        return $ones[$hundred] . ' hundred' . ($remainder ? ' and ' . numberToWords($remainder) : '');
    }
    return '';
}

// 将数字转换为序数词英文
function numberToOrdinal($num) {
    $ordinals = [
        1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',
        6 => 'sixth', 7 => 'seventh', 8 => 'eighth', 9 => 'ninth', 10 => 'tenth',
        11 => 'eleventh', 12 => 'twelfth', 13 => 'thirteenth', 14 => 'fourteenth',
        15 => 'fifteenth', 16 => 'sixteenth', 17 => 'seventeenth', 18 => 'eighteenth',
        19 => 'nineteenth', 20 => 'twentieth', 30 => 'thirtieth', 40 => 'fortieth',
        50 => 'fiftieth', 60 => 'sixtieth', 70 => 'seventieth', 80 => 'eightieth',
        90 => 'ninetieth'
    ];

    if (isset($ordinals[$num])) {
        return $ordinals[$num];
    } else {
        $base = floor($num / 10) * 10;
        $remainder = $num % 10;
        return numberToWords($base) . '-' . $ordinals[$remainder];
    }
}

步骤2:处理出生日期转换的主函数

这个函数接收dd/mm/yyyy格式的日期字符串,拆分后把各部分转换成对应文字再组合:

function dobToWords($dob) {
    // 拆分日期:dd/mm/yyyy
    list($day, $month, $year) = explode('/', $dob);
    
    // 转换日为序数词,首字母大写
    $dayWord = ucfirst(numberToOrdinal((int)$day));
    
    // 获取月份的英文名称(比如5转成May)
    $monthWord = ucfirst(date('F', mktime(0, 0, 0, (int)$month, 1, 2000)));
    
    // 拆分年份为前两位和后两位,分别转成文字
    $yearFirstHalf = substr($year, 0, 2);
    $yearSecondHalf = substr($year, 2, 2);
    $yearWord = ucfirst(numberToWords((int)$yearFirstHalf)) . ' ' . ucfirst(numberToWords((int)$yearSecondHalf));
    
    // 组合所有部分返回
    return "$dayWord $monthWord $yearWord";
}

步骤3:使用示例

$dob = '21/05/1992';
echo dobToWords($dob);
// 输出:Twenty-first May Nineteen Ninety-two

小提示

  • 如果你的日期输入格式是mm/dd/yyyy,记得调整list($day, $month, $year)的顺序为list($month, $day, $year)
  • 函数支持1900-2099范围内的年份转换,完全覆盖常见的出生日期场景
  • 注意英文拼写规范:是nineteen不是ninteen,是ninety不是ninty

内容的提问来源于stack exchange,提问作者shelja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:01:11