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?
Asked
Active
Viewed 2.7k times
7
-
If I understood you correctly, you want to get unix timestamp format? If that's the case just append `->timestamp` like `Carbon::createFromTimestampUTC($scheduleTimestamp)->timestamp;` – b0ne May 17 '17 at 07:40
-
Possible duplicate of [Convert time in HH:MM:SS format to seconds only?](http://stackoverflow.com/questions/4834202/convert-time-in-hhmmss-format-to-seconds-only) – May 17 '17 at 07:41
-
Change `->toTimeString()` to `->format('s')` – aldrin27 May 17 '17 at 07:42
-
1@hopesfall he wants it in Carbon format used by laravel – aldrin27 May 17 '17 at 07:42
-
nope I want whole time in seconds is it possible in carbon with getting involved into regular expression mess – May 17 '17 at 07:45
5 Answers
4
well, its there in carbon itself just use secondsSinceMidnight()
and you are good to go.
$scheduleTimeSeconds = Carbon::createFromTimestampUTC($scheduleTimestamp)->secondsSinceMidnight();
3
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

BM2ilabs
- 522
- 6
- 11
3
private function convertTimeToSecond(string $time): int
{
$d = explode(':', $time);
return ($d[0] * 3600) + ($d[1] * 60) + $d[2];
}
Example, after 2 year...

LavrenovPavel
- 129
- 1
- 3
1
You can try this it will return only the seconds
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->second;
or
$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)-> diffInSeconds();

Sarnodeep Agarwalla
- 237
- 2
- 5
0
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;

Sapnesh Naik
- 11,011
- 7
- 63
- 98
-
-
@BhavikBamania I'm sorry I don't have any idea on how to do it using Carbon library – Sapnesh Naik May 17 '17 at 07:58
-