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);
Related
I have to implement some functionality using time calculation and my app has following type of code.
date_default_timezone_set(auth()->user()->timezone);
$t_now = \Carbon\Carbon::parse(date('Y-m-d H:i:s'));
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time) ;
#endphp
#php
$check = $t_allowed->diffForHumans($t_now);
$search = 'after';
$dff_min = $t_allowed->diffInSeconds($t_now, true);
$init = $dff_min;
$day = floor($init / 86400);
$hours = floor(($init - $day * 86400) / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
$late_not_late = $hours . ' hours ' . $minutes . ' minutes ' . $seconds . ' seconds ';
first i want to confirm that $dff_min = $t_allowed->diffInSeconds($t_now, true); is returning minutes or seconds? Acording to my knowledge $dff_min contain seconds
i know that hours could be calculate using (init /3600) but what is the meaning of following statement
$hours = floor(($init - $day * 86400) / 3600);
why developer subtracting $day * 86400 from $init?
similary we also can calculate seconds by $init/60 since in one minute there are 60 seconds but
what is meaning of following line
$minutes = floor(($init / 60) % 60);
and also why he is using Modulo here
$seconds = $init % 60;
\Carbon\Carbon::parse(date('Y-m-d H:i:s')) will call twice the timelib and will loose the microseconds, just do \Carbon\Carbon::now() and you don't need to reinvent the wheel, you can get this exact string with:
$t_now = \Carbon\Carbon::now();
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time);
$late_not_late = $t_allowed->diffForHumans($t_now, ['parts' => 3, 'syntax' => CarbonInterface::DIFF_ABSOLUTE]);
$late_not_late will contain 2 days 9 hours 20 minutes
This is not a duplicate question, but involves a little understanding about time.
I need a solution to the following problem
I have a number of specifically produced times (based on a date), that need to be rounded to the nearest 15 secs:
60 secs is 1 minute
meaning a regular round, floor, ceiling is to the nearest decimal (10/5)
which doesn't help me with time.
also since I'm dealing with secs, it could be that 59:59 will be rounded up to the nearest hour: e.g. 17:59:59 should be 18:00.
example:
6:17:29 rounded to 6:17:30
6:29:55 rounded to 6:30:00
20:45:34 rounded to 20:45:30
The following code does some of the job:
$hr = date('H',($resultStr));
$mn = date('i',($resultStr));
$sc = date('s',($resultStr));
$tot = ($hr * 60 * 60) + ($mn * 60) + $sc;
$totd = $tot / (60);
$totc = ceil($totd);
$totc = $totc / 60;
$hr = floor($totc);
$mn = ($totc - $hr)*60;
$mnflr = floor($mn);
$mn2 = $mn - $mnflr;
echo "$hr:$mnflr";
This results in:
18:35:17 rounded to: 18:36 (which is wrong)
18:31:49 rounded to: 18:32 (which is wrong)
As an aside:
$secs = date('U',($resultStr));
$round = ceil ( (($secs / 60 ) * 60 ));
$newtime = date('H:i:s',($round));
produces: 18:42:58 rounded to: 18:42:58 which is also incorrect
Please and thank you in advance....
You're massively overcomplicating this, just do rounding on the Unix timestamp level:
function roundMyTime($time)
{
$time = strtotime($time);
$time = 15*round($time/15);
echo date('H:i:s', $time)."\n";
}
roundMyTime('18:35:17');
roundMyTime('18:35:27');
roundMyTime('18:35:37');
roundMyTime('18:35:47');
roundMyTime('18:35:57');
roundMyTime('18:36:07');
roundMyTime('18:36:17');
Outputs:
18:35:15
18:35:30
18:35:30
18:35:45
18:36:00
18:36:00
18:36:15
Demo here.
$seconds = ($hr * 60 + $mn) * 60 + $sc; // convert to seconds
$rounded = round($seconds/15)*15; // round
$sc = $rounded % 60; // get seconds
$mn = ($rounded - $sc) / 60 % 60; // get minutes
$hr = ($rounded - $sc - $mn * 60) / 60; // get hours
Convert the date to seconds using strtotime and then just work in seconds.
$seconds = strtotime($date);
$seconds /= 15;
$seconds = round($seconds);
$seconds *= 15;
$date = date("Y-m-d H:i:s", $seconds);
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');
This is not a duplicate question, but involves a little understanding about time.
I need a solution to the following problem
I have a number of specifically produced times (based on a date), that need to be rounded to the nearest 15 secs:
60 secs is 1 minute
meaning a regular round, floor, ceiling is to the nearest decimal (10/5)
which doesn't help me with time.
also since I'm dealing with secs, it could be that 59:59 will be rounded up to the nearest hour: e.g. 17:59:59 should be 18:00.
example:
6:17:29 rounded to 6:17:30
6:29:55 rounded to 6:30:00
20:45:34 rounded to 20:45:30
The following code does some of the job:
$hr = date('H',($resultStr));
$mn = date('i',($resultStr));
$sc = date('s',($resultStr));
$tot = ($hr * 60 * 60) + ($mn * 60) + $sc;
$totd = $tot / (60);
$totc = ceil($totd);
$totc = $totc / 60;
$hr = floor($totc);
$mn = ($totc - $hr)*60;
$mnflr = floor($mn);
$mn2 = $mn - $mnflr;
echo "$hr:$mnflr";
This results in:
18:35:17 rounded to: 18:36 (which is wrong)
18:31:49 rounded to: 18:32 (which is wrong)
As an aside:
$secs = date('U',($resultStr));
$round = ceil ( (($secs / 60 ) * 60 ));
$newtime = date('H:i:s',($round));
produces: 18:42:58 rounded to: 18:42:58 which is also incorrect
Please and thank you in advance....
You're massively overcomplicating this, just do rounding on the Unix timestamp level:
function roundMyTime($time)
{
$time = strtotime($time);
$time = 15*round($time/15);
echo date('H:i:s', $time)."\n";
}
roundMyTime('18:35:17');
roundMyTime('18:35:27');
roundMyTime('18:35:37');
roundMyTime('18:35:47');
roundMyTime('18:35:57');
roundMyTime('18:36:07');
roundMyTime('18:36:17');
Outputs:
18:35:15
18:35:30
18:35:30
18:35:45
18:36:00
18:36:00
18:36:15
Demo here.
$seconds = ($hr * 60 + $mn) * 60 + $sc; // convert to seconds
$rounded = round($seconds/15)*15; // round
$sc = $rounded % 60; // get seconds
$mn = ($rounded - $sc) / 60 % 60; // get minutes
$hr = ($rounded - $sc - $mn * 60) / 60; // get hours
Convert the date to seconds using strtotime and then just work in seconds.
$seconds = strtotime($date);
$seconds /= 15;
$seconds = round($seconds);
$seconds *= 15;
$date = date("Y-m-d H:i:s", $seconds);
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).