I want to convert the time which I got from $scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString();
which is currently giving me 07:32:40
now I want to convert it into seconds only using Carbon library is this possible to do so if yes then how?
{Carbon} Convert time in HH:MM:SS format into seconds in Laravel?
Asked Answered
well, its there in carbon itself just use secondsSinceMidnight()
and you are good to go.
$scheduleTimeSeconds = Carbon::createFromTimestampUTC($scheduleTimestamp)->secondsSinceMidnight();
you could use
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->diffInMinutes()
to display the time in minute or
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->diffInSeconds()
to display it in second
private function convertTimeToSecond(string $time): int
{
$d = explode(':', $time);
return ($d[0] * 3600) + ($d[1] * 60) + $d[2];
}
Example, after 2 year...
You can try this it will return only the seconds
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->second;
or
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)-> diffInSeconds();
Try this,
$scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString();
$sec = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $scheduleTime);
sscanf($sec, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
looks like I need to go for that core thing nothing in carbon for me? –
Yepez
@BhavikBamania I'm sorry I don't have any idea on how to do it using Carbon library –
Octroi
it's fine I got the answer check my answer :) –
Yepez
© 2022 - 2024 — McMap. All rights reserved.
->timestamp
likeCarbon::createFromTimestampUTC($scheduleTimestamp)->timestamp;
– Fluor->toTimeString()
to->format('s')
– Secundines