Convert track length from Spotify to minutes and seconds [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
seconds to minutes and days to weeks
I wonder how I can convert this value to minutes and seconds: 292.96. The value is from this API from Spotify.
Thanks in advance!

Yes, the value is in seconds, so very simple;
int minutes = val / 60;
int seconds = val % 60;

$time = 292.96;
$minutes = floor( $time / 60);
$seconds = $time - ($minutes * 60); // Can add floor() or do mod (%) to round
echo $minutes . ' ' . $seconds; // 4 minutes 52.96 seconds

Related

How can i convert video time in second and minute [duplicate]

This question already has answers here:
PHP: convert seconds to minutes and hours
(2 answers)
Closed 4 years ago.
Convert video time
$json_array = file_get_contents("https://graph.facebook.com/v2.12/$PID[$FID]/videos?fields=title,length,from,description,created_time,source&limit=10&access_token=");
$json_data=json_decode($json_array,true);
foreach($json_data['data'] as $links){
$video_time = $links['length'];
238.142 this time to convert 3.57
Here is the algorithm:
.142 is irrelevant as you only seem to want minutes and seconds
To calculate the seconds, calculate the modulo by 60
238 % 60 = 58
To calculate the minutes, remove the seconds from your total time and divide by 60
(238 - 58) / 60 = 3
=> 3 minutes and 58 seconds
For this you can simply use the gmdate() function,
echo gmdate("H:i:s", 238.142); //output: 00:03:58
echo gmdate("H.i.s", 238.142); //output: 00.03.58
echo gmdate("i.s", 238.142); //output: 03.58
function vtime($lenth){
$seconds = $lenth;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
if($hours == 0){ return "$minutes:$seconds"; }
elseif($minutes == 0){ return $seconds; }
else { return "$hours:$minutes:$seconds"; }
}
echo vtime('304');

converting milliseconds to amount of time string

So pretty much I'm storing the amount of play time a player has in milliseconds and I need to convert it to the amount of time it equals (string).
I've already tried it but I can't seem to get it to be accurate. I used rounding and it turned out poorly.
Can anybody help me out?
Example: 183547165 -> * days * hours * minutes * seconds
If I'm reading the question right, then I think you want something like this!
<?php
$milliseconds = '183547165';
$time = $milliseconds / 1000;
$days = floor($time / (24*60*60));
$hours = floor(($time - ($days*24*60*60)) / (60*60));
$minutes = floor(($time - ($days*24*60*60)-($hours*60*60)) / 60);
$seconds = ($time - ($days*24*60*60) - ($hours*60*60) - ($minutes*60)) % 60;
echo $days.' days<br>'.$hours.' hours<br>'.$minutes.' minutes<br>'.$seconds.' seconds';
?>
Converts milliseconds to days, hours, minutes, and seconds.
PHP has a date function which do what you want:
date("H:i:s", '183547165');
It outputs:
09:19:25
PHP DATE

Convert Digit Time to seconds [duplicate]

This question already has answers here:
How to convert a "HH:MM:SS" string to seconds with PHP?
(4 answers)
Closed 5 years ago.
How can I convert a digit time into seconds?
I need to compare the time so I though that it is easier to convert the digit time to seconds and compare the seconds later on.
for example:
00:00:33
00:01:33
02:01:33
You can write a custom parser if you are looking for precise seconds from that format.
Something like this should do the trick :
echo hhmmss2seconds("18:24:35");
function hhmmss2seconds($time) {
$digits = explode(":", $time);
$seconds = 0;
$seconds = $seconds + intval($digits[0]) * 3600; // hours
$seconds = $seconds + intval($digits[1]) * 60; // minutes
$seconds = $seconds + intval($digits[2]); // seconds
return $seconds;
}
You want to use strtotime(). This converts "digit time" to Unix time.
$time_in_seconds = strtotime('00:00:33');
Try this:
echo strtotime("1970-01-01 00:00:11 UTC");
Try This its working
<?php
function time_to_seconds($time) {
list($h, $m, $s) = explode(':', $time);
return ($h * 3600) + ($m * 60) + $s;
}
echo time_to_seconds("00:00:33");
echo time_to_seconds("00:01:33");
echo time_to_seconds("02:01:33");
?>
Thank you..

How to convert minutes to hours and minutes (without days) [duplicate]

This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 8 years ago.
I would like to display time in minues as an hour and minutes.
Example 1: I want to display 125 minutes as a 2:05
I know I can to somethink like:
$minutes=125;
$converted_time = date('H:i', mktime(0,$minutes);
This works fine, but if the time is more then 24h it is a problem.
Example 2:
$minutes=1510;
and I want to receive 25:10 (without days), only hours and minutes.
How to do that?
You can use:
$minutes=1510;
$hours = intdiv($minutes, 60).':'. ($minutes % 60);
!!! This only works with php >= v7.xx
Previous answer:
$minutes=1510;
$hours = floor($minutes / 60).':'.($minutes - floor($minutes / 60) * 60);
As simple as that.
$minutes = 125;
$hours = floor($minutes / 60);
$min = $minutes - ($hours * 60);
echo $hours.":".$min;
EDIT: should use floor() instead of round() for getting correct results.
$hours = floor($minutes / 60); // Get the number of whole hours
$minutes = $minutes % 60; // Get the remainder of the hours
printf ("%d:%02d", $hours, $minutes); // Format it as hours:minutes where minutes is 2 digits

is there a built in functionality in php to convert seconds to hh:mm:ss [duplicate]

This question already has answers here:
Convert seconds to Hour:Minute:Second
(30 answers)
Closed 8 years ago.
is there a built in functionality in php to convert seconds to hh:mm:ss
There's no built in function, this will work though:
function formatSeconds($seconds){
$hours = $seconds / (60 * 60);
$minutes = ( $seconds - $hours * 60 * 60) / 60;
$seconds = $seconds - $hours * 60 * 60 - $minutes * 60 - $seconds;
return $hours.':'.$minutes.':'.$seconds;
}
$seconds=3672;
echo date_create()->add(new DateInterval("PT{$seconds}S"))
->diff(new DateTime)
->format('%H:%I:%S');
Just another way that works.
I needed an answer to this recently and I ran across this web page concerning inserting a phpdate into a mysql table. (I bet this is also what you're dealing with.) It seems to indicate that you can use something like
$mysqldate = date( 'H:i:s', time());
This seems to work fine even if the number of seconds exceeds a 24 hour period.
Yes, the strftime() function (manual):
print(strftime("%H:%M:%S", $_seconds_ ))

Categories