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

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

Related

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..

PHP: What is happening in the round() function?

I get a time in seconds from my database (stored as an integer):
$time = $data["USER_TIME"];
And then i do the following:
$hours = round(($time / 3600), 0);
$minutes = round((($time - ($hours * 3600)) / 60), 0);
$seconds = $time - ($hours * 3600) - ($minutes * 60);
And i create a time string after:
$timeString = formatNumber($hours).":".formatNumber($minutes).":".formatNumber($seconds);
function formatNumber($number) {
if($number < 10) {
return ("0".$number);
} else {
return ("".$number);
}
}
But the results are confusing for me:
10 seconds -> 00:00:10
15 seconds -> 00:00:15
20 seconds -> 00:00:20
25 seconds -> 00:00:25
30 seconds -> 00:01:-30
35 seconds -> 00:01:-25
40 seconds -> 00:01:-20
45 seconds -> 00:01:-15
50 seconds -> 00:01:-10
Can someone explain me what is happening here?
Var_dump $data["USER_TIME"] :
10
15
20
25
30
35
40
45
50
Let's try it with floor. You are using round now, which means everything equal to or above .5 becomes the next integer.
$hours = floor($time / 3600);
$minutes = floor(($time - ($hours * 3600)) / 60);
$seconds = $time - ($hours * 3600) - ($minutes * 60);
You can use the gmdate() function instead of using floor() and round().
echo gmdate("H:i:s", 685);

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

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

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

Converting MP3 duration time

I'm using my iTunes library to get data from about 1,100 mp3s and I'm running into a small issue in getting the duration of the library into minutes and seconds.
$duration = 1893642;
$minutes = bcmod(($duration / 60), 60);
$seconds = bcmod($duration, 60);
echo $minutes.":".$seconds; //returns 0:42
The problem is that this specific MP3 is actually 31:42. Any thoughts on why this isn't working?
$minutes = bcmod(($duration / 60), 60);
is taking the minutes modulo 60. Unless your track is over an hour it will always say 0.
You want it to be
$minutes = floor($duration / 60);
Try this function
function formatTime($secs) {
$times = array(3600, 60, 1);
$time = '';
$tmp = '';
for($i = 0; $i < 3; $i++) {
$tmp = floor($secs / $times[$i]);
if($tmp < 1) {
$tmp = '00';
}
elseif($tmp < 10) {
$tmp = '0' . $tmp;
}
$time .= $tmp;
if($i < 2) {
$time .= ':';
}
$secs = $secs % $times[$i];
}
return $time;
}
Not sure if the following function was available when this question was written, but as it's a question I've been asking myself so here goes.
I used the answer above:
$seconds = bcmod($row{'playtime_seconds'}, 60);
$minutes = floor($row{'playtime_seconds'} / 60);
$hours = floor($minutes / 60);
Which works for the majority of times, but there is no padding - so you can end up with 20:1 when it should be 20:01 - and it's not to good over an hour - one length comes in at length="1:70:9" - so an alternative is to use the "date" function.
<?=date("H:i:s", $duration); ?>
which returns 00:31:42 from that number of seconds
$duration_str = sprintf('%s:%02s:%02s',
floor($duration_int / 3600), // hours
floor($duration_int / 60) - floor($duration_int / 3600) * 60, // minutes
$duration_int % 60); // seconds
The *printf functions provide formatting. In this case the leading zero.
The minutes line is the most complex part, since you have to calculate the hours (duration [s] / 3600 [s/h]), then round down to integer (floor()), then multiply with 60 to transform to minutes, then subtract that from the total number of minutes (duration [s] / 60 [s/m]).
If your durations are shorter than an hour, the code is much simpler:
$duration_str = sprintf('%s:%02s', floor($duration_int / 60), $duration_int % 60);
The result is still correct for a duration greater than 59 minutes, but just not as readable (31560 minutes in the example).

Categories