-1

I have a date like 04-01-1965, and I want to change it to words like: Fourth January Nineteen Sixty Five

How can I do it in Laravel?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Umesh Kumar Yadav
  • 121
  • 1
  • 4
  • 14
  • Some of that can be done with https://carbon.nesbot.com/docs/#api-formatting , but I think the year portion might be a bit tricky – aynber Apr 21 '20 at 17:07
  • Carbon provide `Thursday 25th December 1965` not the same I want. – Umesh Kumar Yadav Apr 21 '20 at 17:25
  • Neither PHP nor Laravel/Carbon will do what you want out of the box. You're going to have to create your own solution for it. – aynber Apr 21 '20 at 17:31

1 Answers1

0

Neither Laravel nor PHP will give you this kind of output. You have to write your own code for this. In this case, you can try this:

$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];
// Updated
if($first_half >= 20) {
    if($year[1] == '0')
       $first_half = $tens[$year[0]];
    else 
       $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>";

It will give you output like: Today is fourth january nineteen sixty five

Nazem Mahmud Piash
  • 1,053
  • 1
  • 13
  • 34
  • `Undefined offset: 0` on `$first_half = $tens[$year[0]]." ".$ones[$year[1]];` when enter `25-04-2020` but fine with `$dateString = "04-01-1965";` – Umesh Kumar Yadav Apr 24 '20 at 20:23
  • I have updated the code. And please a suggestion. Don't mind. Try to understand each line of code. Just don't copy and paste. You can't learn in that way. Google more. there are many solutions on Undefined offset error. – Nazem Mahmud Piash Apr 24 '20 at 20:57