{Carbon} 在 Laravel 中将 HH:MM:SS 格式的时间转换为秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44018430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:59:14  来源:igfitidea点击:

{Carbon} Convert time in HH:MM:SS format into seconds in Laravel?

phplaravelphp-carbon

提问by BM2ilabs

I want to convert the time which I got from $scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString();which is currently giving me 07:32:40now I want to convert it into seconds only using Carbon library is this possible to do so if yes then how?

我想这是我得到了来自时间转换$scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString();这是目前给我的07:32:40,现在我想将其转换成秒只使用碳库是这可能这样做,如果是的话怎么办?

采纳答案by LavrenovPavel

well, its there in carbon itself just use secondsSinceMidnight()and you are good to go.

好吧,它在碳本身中就可以使用secondsSinceMidnight(),你很高兴。

 $scheduleTimeSeconds = Carbon::createFromTimestampUTC($scheduleTimestamp)->secondsSinceMidnight();

回答by BM2ilabs

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

在第二个显示它

回答by Sarnodeep Agarwalla

You can try this it will return only the seconds

你可以试试这个它只会返回秒

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->second;or

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->second;或者

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)-> diffInSeconds();

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)-> diffInSeconds();

回答by LavrenovPavel

private function convertTimeToSecond(string $time): int
{
    $d = explode(':', $time);
    return ($d[0] * 3600) + ($d[1] * 60) + $d[2];
}

Example, after 2 year...

例如,2年后...

回答by Sapnesh Naik

Try this,

尝试这个,

$scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString();

$sec = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00::", $scheduleTime);

sscanf($sec, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;