I want to calculate average of time-in for a user for particular duration , i have timestamp values for each time-in .
To calculate average i want to add all timestamps and divide by no of days .
But sum of all timestamps gives wrong input so i want convert timestamps to seconds so i can add them and calculate average .
I am using following code .
$timeInTotalSec = 0;
$timeInTotalSec += intval(date("H",$punchintime)) * 60 * 60;
$timeInTotalSec += intval(date("i",$punchintime)) * 60;
$timeInTotalSec += intval(date("s",$punchintime));`
but
date("H",$punchintime)
gives me proper value but
intval(date("H",$punchintime))
giving me 0
Thanks in advance .
Your question isn't very clear, but I think I understand that you want to calculate the average punch in time from a series of punch in times.
Dates are no good for this, you need to isolate the number of seconds after midnight for each $punchintime
and calculate the average of that. The following code does that. I created an array of times to illustrate my point, I don't know anything about your system, so generating the input array is down to you.
$punchInTimes = array(
'2013-08-01 09:00',
'2013-08-02 09:06',
'2013-08-03 08:50',
'2013-08-04 09:20',
'2013-08-05 09:01',
'2013-08-06 08:56',
);
function getAverageTime(array $times)
{
$seconds = $average = 0;
$result = null;
//get seconds after midnight
foreach($times as $dateString){
$date = new \DateTime($dateString);
list($datePart) = explode(' ', $dateString);
$midnight = new \DateTime($datePart);
$seconds += $date->getTimestamp() - $midnight->getTimestamp();
}
if($seconds > 0){
$average = $seconds/count($times);
$hours = floor($average/3600);
$average -= ($hours * 3600);
$minutes = floor($average/60);
$average -= ($minutes * 60);
$result = new \DateInterval("PT{$hours}H{$minutes}M{$average}S");
} else $result = new \DateInterval('PT0S');
return $result->format("%Hh %Mm %Ss");
}
echo "Average clock in time is " . getAverageTime($punchInTimes);
Output:-
Average clock in time is 09h 00m 10s
This does not work for times that span midnight, such as an array like this:-
$aroundMidnight = array(
'2013-08-01 23:59',
'2013-08-02 00:02',
);
What you are talking about is unixtime. Unixtime is seconds from the unix epoch (January 1, 1970). To get the time different in two timestamps you can simply minus the first timestamp from the second.
$timestamp1 = date('U');
And then some time later:
$timestamp2 = date('U');
Store these variables and when it comes time to get the difference:
$difference = $timestamp2 - $timestamp1;
You can then format the time using basic math:
$seconds = $difference;
$minutes = $seconds/60;
$hours = $minutes/60;
$days = $hours/24;
Hope this helps!
try this code
public static function sec2hms($sec, $padHours = false) {
// start with a blank string
$hms = "";
// do the hours first: there are 3600 seconds in an hour, so if we divide
// the total number of seconds by 3600 and throw away the remainder, we're
// left with the number of hours in those seconds
$hours = intval(intval($sec) / 3600);
// add hours to $hms (with a leading 0 if asked for)
$hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" : $hours . ":";
// dividing the total seconds by 60 will give us the number of minutes
// in total, but we're interested in *minutes past the hour* and to get
// this, we have to divide by 60 again and then use the remainder
$minutes = intval(($sec / 60) % 60);
// add minutes to $hms (with a leading 0 if needed)
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":";
// seconds past the minute are found by dividing the total number of seconds
// by 60 and using the remainder
$seconds = intval($sec % 60);
// add seconds to $hms (with a leading 0 if needed)
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
// done!
return $hms;
}