【问题标题】:Changing date format in my template PhpWord在我的模板 PhpWord 中更改日期格式
【发布时间】:2020-11-26 23:42:47
【问题描述】:

我正在尝试在我的模板 PhpWord 中更改日期格式的输出,但它不起作用。 在我的控制器中,我有:

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"));

}

行内-> $my_template->setValue('date_de_naissance', $desc1->date_de_naissance); 我在输出文档中的日期格式显示为 12-07-2020,但我想要 2020 年 12 月 12 日。 我能怎么做? 感谢您的帮助!

【问题讨论】:

    标签: laravel date-format phpword


    【解决方案1】:

    感谢answer:

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

    更多细节在:

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

    【讨论】:

    • 非常感谢 OMR。你的建议终于奏效了。
    【解决方案2】:

    使用format() 方法格式化日期。

    您必须在日期/时间戳实例(变量)上调用 format() 方法:

    $desc1->date_de_naissance->format('j F Y');
    

    【讨论】:

    • 谢谢 OMi Shah。但它缺乏碳
    猜你喜欢
    • 2014-10-25
    • 2017-10-04
    • 2018-10-15
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2013-12-12
    • 2015-10-25
    相关资源
    最近更新 更多