How to convert seconds into HH:mm format php - php

I am having a time duration in seconds. I want show it in HH:mm format. Is there way to do it using php date-time function??

Use gmdate() and set the seconds as param.
Example:
$seconds = 20;
$result = gmdate('H:i', $seconds);
Edit: Ooops... I placed date instead of gmdate... Now I noticed the problem with the timezones with date.
(It's corrected now.)

If you have seconds you could do:
<?php
$sec = 3864;
$h = floor($sec /3600);
$m = floor(($sec - $h *3600) / 60);
$s = $sec % 60;
printf("%02d:%02d:%02d", $h, $m, $s);
?>
output here: http://codepad.org/7ee9Cx03

i found this solution:
function sec_to_his ($seconds)
{
return gmdate ('H:i:s', $seconds);
}
some other variations can be found on this website: http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
greets stefan.

You could write a function:
<?php
function convertToHoursMinutes($seconds) {
$seconds = intval($seconds);
$hours = ceil($seconds/3600);
$minutes = $seconds%3600;
return sprintf('%d:%02d', $hours, $minutes);
}
echo '24502 seconds is ' . convertToHoursMinutes(24502);
Or you could try searching Google your question.

Related

PHP sum two different minutes

i have two different break time
default break time
extra break time
here i want to sum of two times and display 12 hrs format
EX :
$default_time = "00:30";
$extra_time = "00:25";
my expected output : 00:55
but now display 01:00
this is my code
$default_time = $work_data->break_time;
$break_time = $work_data->extra_time;
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",strtotime($total_break));
Here is the function you can calculate total time by passing the arguments to functions.
$hours, $min are supposed variable which is zero
$default_time = "00:30";
$break_time = "00:25";
function calculate_total_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 calculate_total_time($default_time, $break_time); # 00:55
There is one function call to strtotime function too much.
You should leave out the strtotime() call in the last line, as $total_break already is a UNIX timestamp:
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",$total_break);
The problem is that you're trying to add too specific timestamps, but what you're trying to achieve is adding two durations. So you need to convert those timestamps into durations. For that you need a base, which in your case is 00:00.
$base = strtotime("00:00");
$default_time = $work_data->break_time;
$default_timestamp = strtotime($default_time);
$default_duration = $default_timestamp - $base; // Duration in seconds
$break_time = $work_data->extra_time;
$break_timestamp = strtotime($break_time);
$break_duration = $break_timestamp - $base; // Duration in seconds
$total_break = $default_duration + $break_duration; // 55 min in seconds
// If you want to calculate the timestamp 00:55, just add the base back to it
echo date("H:i", $base + $total_break);
Consider using standard DateTime and DateInterval classes. All you will need is to convert your second variable value to interval_spec format (see http://php.net/manual/en/dateinterval.construct.php for details):
$defaultTime = "00:30";
$breakTime = "PT00H25M"; // Or just 'PT25M'
$totalBreak = (new DateTime($defaultTime))->add($breakTime);
echo $totalBreak->format('H:i');
You could try the following code fragment:
$time1 = explode(":", $default_time);
$time2 = explode(":", $break_time);
$fulltime = ($time1[0] + $time2[0]) * 60 + $time1[1] + $time2[1];
echo (int)($fulltime / 60) . ":" . ($fulltime % 60);
<?php
$time = "00:30";
$time2 = "00:25";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
print_r($result);
?>
Use below code you will definitely get your answers.
$default_time = "00:30:00";
$extra_time = "00:25:00";
$secs = strtotime($extra_time)-strtotime("00:00:00");
$result = date("H:i:s A",strtotime($default_time)+$secs);
echo $result;die;
You can modify above code as per your need.
You could try the following:
$default_time = $work_data->break_time;
$date_start = new DateTime($default_time);
$break_time = $work_data->extra_time;
$interval = new DateInterval("PT" . str_replace(":", "H", $break_time) . "M");
$date_end = $date_start->add($interval);
echo $date_end->format("H:i");
Note that this doesn't account for times which span a 24 hour period

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

Change time output using php

I have a time average total that is computed from a time converted from an integer.
average = 0:0:20
I wanted to change its output like this:
average = 00:00:20
By the way, this is the code I used to get the time average:
$ans = $times / $displaycount;
$hh = floor($ans / 3600);
$mm = floor(($ans - ($hours*3600)) / 60);
$ss = floor($ans % 60);
$timeavg = $hh.':'.$mm.':'.$ss;
echo "average = ". $timeavg;
try
$str= '0:0:20';
echo date('H:i:s', strtotime($str)); //output :- 00:00:20
I would recommend to use format function like sprintf():
$timeavg = sprintf('%02d:%02d:%02d', $hh, $mm, $ss);
demo

php - calculating distance divded by time

How do I divide a decimal by time queried from database as time format.
Any idea?
$time = date($entity->getTime()->format('H:i:s'));
$speed = $distance/$time
Which is definitely wrong and if my time is 00:40:00, I get some division by zero error.
I am unable to convert it to seconds because php takes DateTime from Time format in database.
I propose that you get your time in seconds, but you need to convert minutes and hours to seconds.
$seconds = date($entity->getTime()->format('s'));
$minutes = date($entity->getTime()->format('i'));
$hours = date($entity->getTime()->format('h'));
$time = $hours * 3600 + $minutes * 60 + $seconds;
$speed = $distance/$time;
Checkout strtotime() to convert it to seconds.
Docs: http://php.net/manual/en/function.strtotime.php
With strtotime it is a little bit tricky and only goes to 24:59:59.
Else use Voitcus solution.
$time = '00:40:00';
echo strtotime("1970-01-01 $time UTC");
1) Get time in seconds
function time2seconds($time='00:00:00')
{
list($hours, $mins, $secs) = explode(':', $time);
return ($hours * 3600 ) + ($mins * 60 ) + $secs;
}
$time = date($entity->getTime()->format('H:i:s'));
$timeInSeconds = time2seconds($time);
$distance = 40000;
$speed = $distance/$timeInSeconds;
2) If you are using MySQL database use function TIME_TO_SEC(time)
$time = date($entity->getTime()->format('H:i:s'));
$speed = $distance/$time
It's wrong. You must do:
$speed = $distance/$time * 3.6;
For instance this equation is valid 30km/H = 30000m * 3600 seconds * 3.6

Converting seconds to hours/minutes/seconds in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting Seconds to HH:MM:SS
I have an integer as a number of seconds. I want to convert that integer into into hours/minutes/seconds like this:
1:45:32
If the number of seconds equates to less than one hour then it should return the string:
45:32
If the number of minutes is less than 10 it should return the string formatted like this:
3:25
And finally if the number of seconds equate to less than 1 minute, it should return the following:
0:04
What is the best way to do this in PHP?
The simpelst approch would be
if($seconds < 3600){
$format = 'i:s';
}else{
$format = 'G:i:s';
}
echo date($format, $seconds);
EDIT: this wouldnt fix your minutes < 10 problem.
you could handle the minutes by itself. like
$time = ($seconds >= 3600) ? date('G', $seconds).':' : '';
$time .= intval(date('i',$seconds)).':'.date('s', $seconds);
<?php
$seconds = (1*60 + 45)*60 + 32; // 1:45:32
define("SECONDS_IN_HOUR", 3600);
define("SECONDS_IN_MINUTE", 60);
// hours
if ($seconds >= SECONDS_IN_HOUR)
{
print floor($seconds/SECONDS_IN_HOUR) . ":";
$seconds = $seconds % SECONDS_IN_HOUR;
}
// minutes
if ($seconds >= SECONDS_IN_MINUTE)
{
print floor($seconds/SECONDS_IN_MINUTE) . ":";
$seconds = $seconds % SECONDS_IN_MINUTE;
}
// seconds
print $seconds;
?>
I think Rufinus is pretty close:
foreach(array(60 => ' 0:s', 3600 => 'i:s', 'G:i:s') as $val => $format)
{
if ($seconds < $val) break;
}
echo ltrim(ltrim(gmdate($format, $seconds), '0'), ' ');
This variant uses a configuration stored inside an array which associates a format string based on a time value in seconds (as key). The last element is the default format that will fall through.
Edit: Unfortunately there is no formatting code in date that allows to specify minutes w/o a leading there. Therefore the date string needs to be re-formatted to remove leading 0's occasionally. It's done with ltrim here.
function formatHMS($time) {
$s = $time % 60;
$time= floor($time/60);
$m = $time % 60;
$time= floor($time/60);
$h = floor($time);
$str = $s;
if ($m>0)
$str = "$m:$str";
if ($h>0)
$str = "$h:$str";
return $str;
}
Consider using explode() & implode() and then apply your logic of less-than & greater-than!

Categories