how to add two time variable in php [duplicate] - php

This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 8 years ago.
I want to add 2 time variable in php. I use below code but it returns a big number. how can I have a time like (15:20:00) at the end?
code :
$time1="10:00:00";
$time2="05:20:00";
$sum = strtotime($time1)+strtotime($time2);
echo date("H:i:s",$sum);
Expected result: 15:20:00

Add to the end
$sum = strftime('%H:%M:%S', $sum);
http://php.net/manual/en/function.strftime.php

Try something like :
echo sum_the_time($time1, $time2);
function sum_the_time($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return "{$hours}:{$minutes}:{$seconds}";
}

Related

PHP: Pass Array to Function with 2 acceptable variable only

How can i pass the value in my array to a function that can only accept 2 variable.
function i created
function sum_the_time($time1,$time2){
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time) {
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
when i try to display it gives me the total time:
here how i display the result.
echo sum_the_time('01:45:22', '17:27:03');
where i declared the time.
i want to pass the stored value in my array
here's what i did
$stored_time = array();
for($i=0;$i<count($lang_val);$i++){
$target_time = $project_time;
$stored_time[] = $target_time;
}
this will add a value to my array[].
the time is get from the generated script.
i just want to know how can i pass the value of my array. to the function i created.
sometimes there are 3 value in my array or my 5 or so on.
Rewrite the function and make it accept an array instead.
function sum_the_time($times){
$seconds = 0;
foreach ($times as $time) {
//...
pass $times array directly to the function sum_the_time() as
$times = array($time1, $time2);
function sum_the_time($times)
{
if (count($times) > 0)
{
// your code
}
}

MySQL Time differencials

I am using php and mysql to create an automated html email report. This report shows the total number of emails received and logged into our system by a particular client. This is broken down by month in the given year. It also shows the average turn time from the point the email was said to be received, and the point that the ticket (ticket is logged into our database with information regarding email) is closed. Currently, I am using the following query and function to get the average turn time each month. I was told that they want to calculate the turn time based upon business hours, 8-8, not a full 24 hour day.
How can I calculate an average turn time between assuming that the time allotted in a day is 12 hours (8-8)?
If an email was received 21:00:00 Monday, and the ticket was closed 8:05:00 Tuesday, the turn time would equal 00:05:00 or 5 minutes.
$year = date("Y");
$month_num = date('m');
$total_time = "00:00:00";
for($m = 1; $m<=12; $m++)
{
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
$turn_time = GetTurnTime($month, $year);
$message .= '<td align="center">'.$turn_time['Time'].'</td>';
$total_time = sum_the_time($total_time, $turn_time['Time']);
$average_time = sum_the_average($total_time, $turn_time['Time'], $month_num);
}
function GetTurnTime($month, $year)
{
$turn_time = mysql_fetch_assoc(mysql_query("SELECT sec_to_time(AVG(SEC_TO_TIME(time_to_sec(TIMEDIFF(closed_on, received_on))))) AS `Time`, DATE_FORMAT(received_on, '%m/%d/%Y') as `Date` from `calltrak`.`calls` where monthname(received_on) = '{$month}' and year(received_on) = '{$year}' and ticket_status = 'CLOSED' and ticket_source = '2' and dept_id in (select dept_id from depts where bus_id = '4') and cust_name = 'SOMECOMPANY'"));
return $turn_time;
}
function sum_the_time($time1, $time2)
{
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
function sum_the_average($time1, $time2, $month)
{
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$seconds = $seconds / $month;
round($seconds);
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}

how to sum 2d array

Good evening
I have a question here. i have two arrays: $duration_c[$k][$s]. it prints all ok
$duration_c[0][0]="19:30:00";
$duration_c[0][1]="00:10:00";
$duration_c[1][0]="00:30:00";
$duration_c[1][1]="00:20:00";
than to sum
$times=$duration_c[$k][$s];
function sum_the_time($times) {
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
// return "{$hours}:{$minutes}:{$seconds}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
echo sum_the_time($times);
how to sum $duration_c[0][0]+$duration_c[0][1] and $duration_c[1][0]+$duration_c[1][1], and how to print?
there is error Invalid argument supplied for foreach()
you are sending only one value to sum_the_time function as $times=$duration_c[$k][$s];
$times has only one value you can print and check what value is going to the function by writing like this
$times=$duration_c[$k][$s];
echo $times;
use
$times=$duration_c[$k];
so that you can send array to your function and foreach will not give you error
$times=$duration_c[$k]; // contains array of times
function sum_the_time($times) {
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
// return "{$hours}:{$minutes}:{$seconds}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
echo sum_the_time($times);
This does it less in PHP code, and keeps the execution flow as much as possibly in the PHP runtime, written in C:
<?php
$duration_c[0][0]="19:30:00";
$duration_c[0][1]="00:10:00";
$duration_c[1][0]="00:30:00";
$duration_c[1][1]="00:20:00";
$sum = 0;
foreach($duration_c as $durations) {
$today = strtotime('today');
$sum += array_sum(array_map(function($str) use ($today) {
return strtotime($str)-$today;
}, $durations));
}
echo $sum; // 73800 seconds, that's 20.5 hours
$times=$duration_c[$k][$s]; should be $times=$duration_c[$k];, otherwise you are passing a single time to the function, not an array of times.

Add a list of times in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working on something where I need to add a list of times togther in the following format mm:ss so 3:10 would be 3 minutes and 10 seconds.
So how for example would you go about the following sum in php?
2:10 + 3:15 + 6:59 + 2:22
Use following code :
$tarr = array('2:10', '3:15','6:59','2:22');
echo sum_the_time($tarr);
function sum_the_time($times) {
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
// $seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
// return "{$hours}:{$minutes}:{$seconds}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
Here is the demo : http://codepad.org/dJBY78wZ
Just for fun :)
function sumTime($times) {
$min = $sec = 0;
foreach($times as $time) {
list($cmin, $csec) = explode(':', $time);
$min += $cmin;
$sec += $csec;
}
return sprintf('%02d:%02d', $min + floor($sec/60), $sec % 60);
}
http://viper-7.com/txkHAs
<?php
function s($str) {
list($minutes,$seconds) = explode(':',$str);
return $minutes * 60 + $seconds;
}
function ms($seconds) {
return sprintf('%02d:%02d', $seconds/60, $seconds%60);
}
$seconds = s('2:10') + s('3:15') + s('6:59') + s('2:22');
echo ms($seconds);
http://codepad.viper-7.com/BUkRRM

adding two time values of similar formats using php

i have two time values as give below
$time = 06:58:00;
$time2 = 00:40:00;
I am doing this for calculating the appointments and available time for a particular user
so i tried in this way
$max_date=abs(strtotime($time) + strtotime($time2));
but it is returning $max_date =2673452280
any suggestions pls
this code sample would take hour in $time and add the hour in $time2 to it
for example: time=06:58:00, time2=00:40:00, result = 07:38:00
$time = "06:58:00";
$time2 = "00:40:00";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
Use this function...
function sum_the_time($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
if($seconds < 9)
{
$seconds = "0".$seconds;
}
if($minutes < 9)
{
$minutes = "0".$minutes;
}
if($hours < 9)
{
$hours = "0".$hours;
}
return "{$hours}:{$minutes}:{$seconds}";
}
strtotime function takes full-date as an argument and valid format are as following:
http://www.php.net/manual/en/datetime.formats.php
You can see that in online PHP manual for the function at http://php.net/manual/en/function.strtotime.php
If you're build those time strings from a database before, you'd probably want to rebuild them to something like this:
$time = "00:06:58";
$time2 = "40 minutes";
$timestamp = strtotime($time." +".$time2);
$endTime = date("d.m.Y H:i:s", $timestamp);
Easiest way to add two times using php is :
1) Convert time from H:i:s (e.g. 08:15:40) format to seconds.
2) do the same for second time value ref:step 1
3) add converted values and store it php variable
4) Now convert total (which is in seconds) to H:i:s
and it works for me.
PHP Script:
$str_time ="08:04:40";
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$hrs_old_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$str_time ="02:10:22";
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$hrs_toadd_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$hrs_old_int1 = $hrs_old_seconds + $hrs_toadd_seconds;
echo $Total=gmdate("H:i:s", $hrs_old_int1);
Result= :10:15:02
Anudeep's solution was great for my use case, but I needed to be able to add negative times as well. Here's a slightly edited version of his code to take and return negative time strings ("-01:01:01" for example):
public static function sum_the_times($time1, $time2)
{
$times = array($time1, $time2);
$seconds = 0;
$negative = false;
foreach ($times as $time) {
list($hour,$minute,$second) = explode(':', $time);
if(substr($hour,0,1) == '-'){
$seconds -= substr($hour,1)*3600;
$seconds -= $minute*60;
$seconds -= $second;
} else {
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
}
if (substr($seconds, 0, 1) == '-') {
$negative = true;
$seconds = ($seconds * -1);
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
if ($seconds < 9) {
$seconds = "0".$seconds;
}
if ($minutes < 9) {
$minutes = "0".$minutes;
}
if ($hours < 9) {
$hours = "0".$hours;
}
return ($negative ? "-" : "")."{$hours}:{$minutes}:{$seconds}";
}
You can try this
$time = "04:00:00";
$time2 = "03:30:00";
$result = date("H:i:s",strtotime($time)+strtotime($time2));
echo $result;
It gives output 07:30:00 but it does not work sometime in different version of operating system. If you want to get sum of time then you can use this code
<?php
function CalculateTime($time1, $time2) {
$time1 = date('H:i:s',strtotime($time1));
$time2 = date('H:i:s',strtotime($time2));
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
if($seconds < 9)
{
$seconds = "0".$seconds;
}
if($minutes < 9)
{
$minutes = "0".$minutes;
}
if($hours < 9)
{
$hours = "0".$hours;
}
return "{$hours}:{$minutes}:{$seconds}";
}
$time1= '23:32:05';
$time2 = '01:29';
echo CalculateTime($time1,$time2);
?>
In the second code, you can send time in hour:minutes or hours:minutes:seconds. This code accept both format because it convert time automatically
Here's a version that will cater for over 24 hours and doesn't use strtotime:
$time0 = "24:01:02";
$time1 = "01:02:03";
$matches0 = explode(':',$time0); // split up the string
$matches1 = explode(':',$time1);
$sec0 = $matches0[0]*60*60+$matches0[1]*60+$matches0[2];
$sec1 = $sec0+ $matches1[0]*3600+$matches1[1]*60+$matches1[2]; // get total seconds
$h = intval(($sec1)/3600);
$m = intval(($sec1-$h*3600)/60);
$s = $sec1-$h*3600-$m*60;
echo $str = str_pad($h, 2, '0', STR_PAD_LEFT).':'.str_pad($m, 2, '0', STR_PAD_LEFT).':'.str_pad($s, 2, '0', STR_PAD_LEFT);

Categories