-2

I have a @foreach on my blade:

@foreach
{{ $value-> StartDate}}
@endforeach

the StartDate is currently displaying like 'sept 2020 12:00:00AM' I want it do be formatted as 'dd/mm/yy'/'d/m/y' essentially like 23-09-2020 or 23/09/2020

I have tried

{{ $value->StartDate->format('d,m,y')}}

and

{{ Carbon\Carbon::parse($value->StartDate)->format('d,m,y') }}

and also changing 'd,m,y' to 'dd,mm,yyyy' etc but I ma getting error. the main error I get is

Could not parse 'Sep 15 2020 12:00:00:AM': DateTime::__construct(): Failed to parse time string (Sep 15 2020 12:00:00:AM) at position 20 (:): Unexpected character

is there a proper way to change the formatting?

UPDATE with more detail :

Im attempting to change how some dates are displayed on my blade file. they are coming from the sql database as 'Sep 16 2020 12:00:00:AM' i want them to display as '16-09-2020' Below is the attempt I have made, however I am still getting this error

Unexpected data found. Unexpected data found. The separation symbol could not be found Trailing data

Does anyone have any ideas on how to resolve this error?

my model :

    class HomeModel extends Entity
{
    protected $dates = [
        'StartDate',
    ];

    public static function findBy(
        \DateTime $StartDate,
        \DateTime $EndDate
    ): array
    {
        $params = [
            $StartDate,
            $EndDate,

        ];
        return self::hydrate(DB::select('EXEC Table @StartDate = ?, @EndDate = ?',
            $params
        ));
    }
}

my controller :

   class Controller extends Controller
{
    public function __invoke(Request $request)
    {
        if (!$request->input('StartDate') && !$request->input('EndDate')) {
            $StartTime = Carbon::now()->subDays(8);
            $EndTime = Carbon::now();
        } else {
            $StartTime = Carbon::parse($request->input('StartDate'));
            $EndTime = Carbon::parse($request->input('EndDate'));
        }
        $data = HomeModel::findBy(
            Carbon::parse($StartTime),
            Carbon::parse($EndTime)
        );

        return view ('home', compact('data'));
    }

my blade :

    <tbody class="list">
                    @foreach($data as $value)
                        <tr>
                            <td class="end-time">
                                {{ \Carbon\Carbon::parse($value->StartDate)->format('d/m/Y')}}
                            </td>
                        </tr>
                    @endforeach
                </tbody>

I have attempted the solution given here but it returns the error of

DateTime::__construct() expects parameter 2 to be string, array given

which was expected as I am trying to get the dates from an array. my attempts of turning the array into a string were also futile. Does anyone know of any php functions for formatting dates in an array?

  • 2
    `12:00:00:AM` has an extra colon between the 00 and AM. You'll need to either fix that or change how you're parsing it. Carbon has a [createFromFormat](https://carbon.nesbot.com/docs/#api-instantiation) function to specify the format. – aynber Sep 23 '20 at 14:41
  • the format from the database table is 'm-d-Y H:i:s', it displays that way, would I still need to use createFromFormat ? – codingWithNikki Sep 23 '20 at 15:18

2 Answers2

3

Can you try the PHP createFromFormat function?

<?php

$date='sept 2020 12:00:00AM';   //dd/mm/yy
$new= \DateTime::createFromFormat('M Y H:i:sA', $date )->format('d/m/Y'); 
echo $new;

Or you can parse the date as below.

\Carbon\Carbon::parse($date)->format('Y-m-d H:i');

Check the demo

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
0

I found a very simple solution. There wasnt an issue with my code but where I was running it.

{{ Carbon\Carbon::parse($value->StartDate)->format('d-m-Y') }}

works great in the blade and will format it correctly. the issue I had was I was not working withing laradock, which is why I was getting the error of

Could not parse 'Sep 15 2020 12:00:00:AM': DateTime::__construct(): Failed to parse time string (Sep 15 2020 12:00:00:AM) at position 20 (:): Unexpected character

After running docker-compose up in the terminal, I stopped getting errors and the dates were formatted correctly. My takeaway is that if you stumble apon this error, check your development area.