how can I just get the format in minutes instead of for example 01:10:33 to get 70:33, or 00:30:01 to get 30:01.
i get value $sec from array in seconds
["Duration"]=>int(5382) or
["No_ofMinutes"]=> string(4) "89.7"
This is my code,
echo gmdate('H:i:s', $sec);
and I need only ('mm:ss')
thank you all for your help.
You can get minutes like following
gmdate("i")
I think you can generate it from the $sec.
$sec = 1801;
$m = floor($sec / 60);
$s = $sec % 60;
$m = $m < 10 ? '0' . $m : $m;
$s = $s < 10 ? '0' . $s : $m;
$time = $m . ':' . $s;
echo $time;
Related
I have to convert time in format of hh:mm:ss which is over 24hr, for instance 34:23:00. Is there any simple solution for this without using explode + strtotime functions?
This should give you what you're looking for (assuming 1 second = 1000000 microseconds).
$time = '34:23:00';
$parts = explode(':', $time);
$hours = !empty($parts[0]) ? $parts[0] : 0;
$minutes = !empty($parts[1]) ? $parts[1] : 0;
$seconds = !empty($parts[2]) ? $parts[2] : 0;
$microseconds = (($hours * 60 * 60) + ($minutes * 60) + ($seconds)) * 1000000;
This should do the trick
<?php
$inputTime = "34:23:00";
$timeSplitted = explode(":", $inputTime);
$microSeconds = ($timeSplitted[0]*3600 + $timeSplitted[1]*60 + $timeSplitted[2]*1)*1000;
var_dump($microSeconds); // int(123780000)
I try calculate time of an act in second with 2 decimals.
protected function microtimeFormat($data)
{
$duration = microtime(true) - $data;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
return $seconds = $duration-$hours*60*60-$minutes*60;
}
this method get start time as $data...and get back it an int second
for example it return 2second.
I try get second with 2 decimals ...
protected function microtimeFormat($data,$format=null,$lng=null)
{
$duration = microtime(true) - $data;
$hours = (float)($duration/60/60);
$minutes = (float)($duration/60)-$hours*60;
$seconds = $duration-$hours*60*60-$minutes*60;
return number_format((float)$seconds, 2, '.', '');
}
but it return me 0.00 for short time
I think your issue comes from the (float) conversion to $hours and $minutes. When you do so you don't save the decimal part of each so your calculation of $seconds always give 0. Convert to int so you actually save in $hours and $minutes the actual number of seconds they each represent. And the remainder goes to $seconds.
protected function microtimeFormat($data,$format=null,$lng=null)
{
$duration = microtime(true) - $data;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = $duration-$hours*60*60-$minutes*60;
return number_format((float)$seconds, 2, '.', '');
}
$start = microtime(TRUE);
sleep(1);
$delay = $this->microtimeFormat($start);
var_dump($delay);
This gives me:
string(4) "1.01"
I use this form to generate a time in seconds, eg 1.20
$start = microtime(true);
for ($i=0; $i < 10000000; $i++) {
# code...
}
$end = microtime(true);
echo "<br>" . $time = number_format(($end - $start), 2);
// We get this: 1.20
An example comparing the performance of 2 functions of PHP:
define( 'NUM_TESTS', 1000000);
$start = microtime(true);
for( $i = 0; $i < NUM_TESTS; $i++)
{
mt_rand();
}
$end = microtime(true) - $start;
echo 'mt_rand: ' . number_format(($end), 2) . "\n";
$start = microtime(true);
for( $i = 0; $i < NUM_TESTS; $i++)
{
uniqid();
}
$end = microtime(true) - $start;
echo 'uniqid: ' . number_format(($end), 2) . "\n";
// We get this: mt_rand: 0.12 uniqid: 2.06
I need to convert time 28:10:10 (in HH:MM:SS) to 1:04:10:10 (in Day:HH:MM:SS). i want to convert input time to 24 hours time format using apply strtime() function.
Also I need to substract a given time from it.
**I have tried to answer your question. below is the code that will give you correct output. pls have a look on the below code.**
function secondsToTime($ss) {
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
// Ensure all values are 2 digits, prepending zero if necessary.
$s = $s < 10 ? '0' . $s : $s;
$m = $m < 10 ? '0' . $m : $m;
$h = $h < 10 ? '0' . $h : $h;
$d = $d < 10 ? '0' . $d : $d;
return "$d:$h:$m:$s";
}
$time = "28:10:10";
$timeArr = array_reverse(explode(":", $time));
$seconds = 0;
foreach ($timeArr as $key => $value)
{
if ($key > 2) break;
$seconds += pow(60, $key) * $value;
}
$seconds;
print secondsToTime($seconds);
OUTPUT 1:04:10:10
I need to add multiple time values as in Hours:mins, so I use
strtotime($value1) + strtotime($value2)
to add all of them, how do I put them back as hours:mins ?
cant use
date("h:i")
it only works if hours < 24.
I appreciate your help. Thanks
Here is an function that will sum all your time values in format HH:MM:
function sum_time() {
$i = 0;
foreach (func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if ($h = floor($i / 60)) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo sum_time('01:05', '00:02', '05:59'); # 07:06
demo
Try this :
function time_convert($s) {
$m = 0; $hr = 0; $td = "now";
if ($s > 59) {
$m = (int)($s/60);
$s = $s-($m*60); // sec left over
$td = "$m min";
}
if ($m > 59) {
$hr = (int)($m / 60);
$m = $m - ($hr*60); // min left over
$td = "$hr hr";
if ($hr > 1) {
$td .= "s";
}
if ($m > 0) {
$td .= ", $m min";
}
}
return $td;
}
And use it:
$time = (int) strtotime($v1) + strtotime($v2);
echo time_convert($time);
May it helps
The function strtotime() returns the time in seconds since January 1 1970 00:00:00 UTC. So adding the return value of this function might not do what you would expect.
Instead of using the date functions we can manipulate the string and perform some basic arithmetic operations:
<?php
$value1 = "12:44";
$value2 = "13:47";
$arr1 = explode(':', $value1);
$arr2 = explode(':', $value2);
$totalMinutes = (int)$arr1[0] * 60 + (int)$arr1[1] + (int)$arr2[0] * 60 + (int)$arr2[1];
$hours = (int) ($totalMinutes / 60);
$minutes = $totalMinutes % 60; // Modulus: remainder when dividing with 60
echo $hours . ':' . $minutes;
?>
Another way with DateTime
$dt1 = new DateTime($value1);
$dt2 = new DateTime($value2);
$interval = $dt1->diff($dt2);
echo $interval->format('%a day(s) %h hour(s) %i minute(s)') . '<br />';
echo ($interval->format('%a') * 24 + $interval->format('%h')) . ' hour(s) ';
echo $interval->format('%i minute(s)');
I would like to ask of this kind of double data type can be converted into seconds?
In my database, the record is 6.80 means that 6 is hours and 80 is minutes. I want the output be 7 hours and 20 minutes. Is this possible?
Do the OOP way..!
For a 12-Hr Format.
<?php
$dt='6.80';
$date = DateTime::createFromFormat('H.i', $dt);
echo $date->format('g')." hours and ".$date->format('i')." minutes";
OUTPUT:
7 hours and 20 minutes
For a 24-Hr Format.
<?php
$dt='23.75';
$date = DateTime::createFromFormat('H.i', $dt);
$var= ($date->format('G')==0)?'00':$date->format('G');
echo $var." hours and ".$date->format('i')." minutes";
OUTPUT:
00 hours and 15 minutes
Example:
$t=6.80;
$s = $t - floor($t);
if($s>.60)
{
$t = $t -$s;
$t++;
$s=$s-.60;
$t=$t+$s;
}
floor() function is used to get the fractional part of a floating point number.
Try this
$str = "6.80";
$str = explode(".",$str);
$min = $str[0]*60 + $str[1];
$sec = $min*60;
echo date("H:i",$sec);
(Or)
$hour = intval($min/60);
$mins = $min%60;
echo "$hour:$mins";
this is another way:
<?php
$time = '9.102';
$tmp_min = strstr($time, '.');
$tmp_hour = strstr($time, '.', true);
$pos = strpos($time, '.');
$str_mins = substr($time, $pos+1);
$min = (int) ($str_mins);
if ( $min > 60 ) {
$ext_hour = floor($min/60);
$final_min = $min%60;
$final_hour = (int) ($tmp_hour + $ext_hour);
echo $final_hour . ' Hours and ' . $final_min . ' Minuts..';
}
else {
$final_hour = (int) ($tmp_hour);
$final_min = $min;
echo $final_hour . ' Hours and ' . $final_min . ' Minuts..';
}
?>