简体   繁体   中英

How to change date format in PHP Laravel?

How I can change date format in PHP Laravel.

This is my output:

我的输出

My output shows the date format in MM/DD/YYYY. So I want to change the date format into YYYY/MM/DD.

Here my code in controller:

public function update(Request $request)
{
    $sites = Sites :: pluck ('sites_id' ,'site_code' , 'site_name');

    $request->validate([
        'dt_range' => 'required',
        'sites_id' => 'required',
        
    ]);

    $query = $request-> only(['dt_range','sites_id']);

    $dt_range = date('Y-m-d ');
    $newDate = \Carbon\Carbon::createFromFormat('Y-m-d ', $dt_range) ->format('Y-m-d');
    dd($query);

    return view ('query.index',compact('sites'));

}

Try this

    $date = '03/01/2021 - 03/01/2021';

    $results = [];

    preg_match_all('#\d{2}/\d{2}/\d{4}#', $date, $results);

    $formattedDate = array_map(function ($date) {
        return date('Y/m/d', strtotime($date));
    }, $results[0]);

    $date = "{$formattedDate[0]} - {$formattedDate[1]}";

result

"2021/03/01 - 2021/03/01"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM