Change time output using php - 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

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

How do I add 45 minutes to an existing time?

I am trying to add two time using strtotime.i use following code to add two time.
my expected output is 15:59:00 but it gives me 09:30:44. so what is the problem?
Thanks in advance.
$json['time'] = "15:14";
$total_duration = "45"; //in minutes
/* convert minutes into hours-minutes */
$hours = intval($total_duration / 60);
$mins = $total_duration % 60;
$service_duration = strtotime($hours."00:00")+strtotime("00:".$mins.":00");
/* end time = total service duration + start time */
$start_time = strtotime($json['time'].":00");
echo $service_duration."--".$start_time."==";
$end_time2 = $start_time+$service_duration;
$end_time = date('H:i:s', $end_time2);echo $end_time;exit;
You can do like
$json['time'] = "15:14";
$total_duration = "45"; //in minutes
$endTime = strtotime("+".$total_duration." minutes", strtotime($json['time']));
echo date('H:i:s', $endTime);
Output:
15:59:00

PHP: Time/HH:Minute:Seconds convert to Seconds

I have a code where it will subtract the Total Duration and the Total Time, and after that the result for the computation will be converted into seconds...
Assuming in my Total Duration is "02:00:00"
then for Total Time is "01:30:00"
For computation...
02:00:00 - 01:30:00 = 00:30:00
then for the result, "00:30:00" will be converted to seconds and the result is "1800"
How can I convert it?
Thanks for the help...
Use strtotime function. It returns the UNIX timestamp (number of seconds since January 1st 1970 00:00:00). If you'll pass the hour format HH:MM:SS to it, you can easily do the math
$to = strtotime('02:00:00');
$from = strtotime('01:30:00');
$seconds = $to - $from; // outputs 30
You assumed that the format is minutes:seconds:miliseconds and you wanted to receive 30 seconds in your case. Actually the output is 30 minutes. Miliseconds are separated with a dot.
Your hours should probably look like this:
$to = strtotime('00:02:00');
$from = strtotime('00:01:30');
How about splitting the Time-String into three substrings with the function (returns an array of substrings)
$substrings = new Array();
$substrings = explode(":", $timeString);
Now the array $substrings contains three substrings (hours, minutes, seconds).
you could compute the seconds just by multiplicating:
$hours = intval($substrings[0]);
$minutes = intval($substrings[1]);
$seconds = intval($substrings[2]);
$seconds = $hours * 3600 + $minutes * 60 + $seconds;
Can you try this,
$start = '01:30:00';
$end = '02:00:00';
$workingHours = (strtotime($end) - strtotime($start));
$res= date("i", $workingHours);
echo "DIFF: ". $res; //OP 30 Minutes
echo $resFull= date("H:i:s", $workingHours); //OP 00:30:00
If you use format HH:MM:SS then you can convert it to seconds by next code
$timestr = "00:30:00";
$temp = explode(":", $timestr);
if ($temp && is_array($temp) && count($temp) == 3) {
$time = intval($temp[0]) * 3600 + intval($temp[1]) * 60 + intval($temp[1]);
} else {
$time = null;
}
Alternative with PHP 5.3:
<?php
try {
$date1 = new DateTime('02:00:00');
$date2 = new DateTime('01:30:00');
$diff = $date1->diff($date2);
echo $diff->format('H:i:s');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}

Adding up time in php

I want to add up time in php but after hours of google'ing and trying out im still unable to find a solution.
my values are:
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
I want to add those two up which make 01:01:00.
Can you give me an example i'm getting really desperate! :p
thanks in advance,
Use strtotime() to turn them into Unix timestamps, then add them as integers:
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
$total = strtotime($newTotal) + strtotime($oldTotal);
To format it as hh:mm:ss again, use date():
echo date('H:i:s', $total);
This gives:
01:01:00
If these values always look like that, you could break them down with a substr()
$hours1 = substr($newTotal, 0, 2);
etc. And then simply add up the seconds, do a divide and mod and bubble up to the hours, and voila!
$secondstotal = $seconds1+$seconds2;
$restseconds = $secondstotal % 60;
$minutesfromseconds = floor($restseconds / 60);
$minutestotal = $minutes1+$minutes2+$minutesfromseconds;
etc.
keep a start date for minimum error.
<?php
$origin = '00:00:00';
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
$added = strtotime($newTotal) + (strtotime($oldTotal) - strtotime($origin));
echo date('H:i:s', $added );
output :
01:01:00
Note, if your time is more than 23:59:59 after adding, you will get wrong result.
Another solution without time function:
function sumtotal($a,$b) {
$i = explode(':',$a);
$j = explode(':',$b); // 0hh:1mm:2ss
$k = array(0,0,0,0); // 0days:1hours:2minutes:3seconds
$k[3] = $i[2]+$j[2];
$k[2] = (int)($k[3]/60)+$i[1]+$j[1];
$k[1] = (int)($k[2]/60)+$i[0]+$j[0];
$k[0] = (int)($k[1]/24);
$k[3] %= 60;
$k[2] %= 60;
$k[1] %= 24;
if ($k[3]<10) $k[3] = '0'.$k[3];
if ($k[2]<10) $k[2] = '0'.$k[2];
if ($k[1]<10) $k[1] = '0'.$k[1];
return $k[0].' days : '.$k[1].' hours : '.$k[2].' minutes : '.$k[3].' seconds';
}
$newTotal = '01:45:21';
$oldTotal = '03:16:56';
echo sumtotal($newTotal,$oldTotal); // result: 0 days : 05 hours : 02 minutes : 17 seconds

How to convert seconds into HH:mm format 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.

Categories