Laravel Carbon cast datetime to set seconds to 00

Viewed 4247

If I have variables like this:

$arivalTime1 = '2020-06-05 15:52:27'
$arivalTime2 = '2020-06-05 15:52:55'

how can I convert these variables using Carbon to have the seconds displayed as 00:

$converted1 = 2020-06-05 15:52:00
$converted2 = 2020-06-05 15:52:00
3 Answers

there is many ways to do it:

1- like Vladimir verleg 's answer:

$arivalTime1 = '2020-06-05 15:52:27'
Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');

2- using startOfMinute() method:

Carbon::parse($time)->startOfMinute()->toDateTimeString();

3- using create method witch gave you more control:

$value=Carbon::parse($time);
$wantedValue=Carbon::create($value->year,$value->month,$value->day,$value->hour,$value->minute,0);

Thank you @OMR for your solution given in the comments:

Carbon::parse($time)->startOfMinute()->toDateTimeString()

Parse your date-string using Carbon and format it, setting the seconds part to 00:

$arivalTime1 = '2020-06-05 15:52:27';
echo Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');
Related