Search code examples
laraveldate-formatphpword

Changing date format in my template PhpWord


I'm trying to chnge the output of my date format in my template PhpWord but it doesn't work. In my Controller I have:

public function edit(Attestationstagiaire $attestationstagiaire, $downloadName = null)
{
    $id = $attestationstagiaire->id;
    $desc1 = Attestationstagiaire::find($id);

    $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templates/stagiaire/attestation.docx'));

    $my_template->setValue('id', $desc1->id);
    $my_template->setValue('prenoms', $desc1->prenoms);
    $my_template->setValue('nom', $desc1->nom);
    $my_template->setValue('date_de_naissance', $desc1->date_de_naissance);
    $my_template->setValue('lieu_de_naissance', $desc1->lieu_de_naissance);

    try{
        $my_template->saveAs(storage_path("Document.docx"));
    }catch (Exception $e){
        //handle exception
    }
    return response()->download(storage_path("Document.docx"));

}

In the line -> $my_template->setValue('date_de_naissance', $desc1->date_de_naissance); My date format in the output document shows 12-07-2020 but I want to have 12 Juillet 2020. How can I do? Thanks for your Help!


Solution

  • thanks to this answer:

    -> $my_template->setValue('date_de_naissance',
    Carbon\Carbon::parse( $desc1->date_de_naissance)->format('d F Y'))
    

    more details in:

    http://php.net/manual/pl/function.date.php