如何在Laravel中将日期更改为Word格式?

问题描述 投票:0回答:1

我想将04-01-1965改成类似Fourth January Nineteen Sixty Five的词

我如何在Larvel中做到这一点?

php laravel laravel-blade
1个回答
0
投票

Laravel和PHP都不会提供这种输出。您必须为此编写自己的代码。在这种情况下,您可以尝试以下操作:

$th = array( 
1 => "first", 2 => "second", 3 => "third", 4 => "fourth", 5 => "fifth", 6 => "sixth", 
7 => "seventh", 8 => "eighth", 9 => "nineth", 10 => "tenth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth", 20 => "twentyth" 
); 

$ones = array( 
1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 
7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 
14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen" 
); 

$tens = array( 
1 => "ten",2 => "twenty", 3 => "thirty", 4 => "forty", 5 => "fifty", 
6 => "sixty", 7 => "seventy", 8 => "eighty", 9 => "ninety" 
); 

$dateString = "04-01-1965";

$day = date("j", strtotime($dateString));
if ($day <= 20) $day = $th[$day];
if($day > 20 ){
    $day = strval($day);
    $second = intval($day[1]);
    $str1 = $tens[intval($day[0])];
    $str2 = $th[intval($day[1])];
    $day = $str1." ".$str2;
}

$month = date("F", strtotime($dateString));
$year = strval(date("Y", strtotime($dateString)));

$first_half = intval($year[0].$year[1]);
if($first_half < 20 ) $first_half = $ones[$first_half];
if($first_half >= 20) {
   $first_half = $tens[$year[0]]." ".$ones[$year[1]];
}

$second_half = intval($year[2].$year[3]);
if($second_half < 20 ) $second_half = $ones[$second_half];
if($second_half >= 20) {
   $second_half = $tens[$year[2]]." ".$ones[$year[3]];
}

$years = $first_half." ".$second_half;
echo "Today is " . $day." ".strtolower($month)." ".$years."<br>";

它将为您提供输出,例如:Today is fourth january nineteen sixty five

© www.soinside.com 2019 - 2024. All rights reserved.