Mister Verleg
Mister Verleg

Reputation: 4293

Laravel Carbon cast datetime to set seconds to 00

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

Upvotes: 4

Views: 7044

Answers (3)

OMR
OMR

Reputation: 12188

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

Upvotes: 6

user8034901
user8034901

Reputation:

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

Upvotes: 1

Mister Verleg
Mister Verleg

Reputation: 4293

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

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

Upvotes: 1

Related Questions