I am trying to get a total time from few times written as a strings in php.
Times as strings:
00.25, 07.35, 01.10, 00.00, 01.00, 00.22
With that I should get 10hrs12mins but I get 11hrs03mins.
function add()
{
$midnight = strtotime('0.00');
$extra = strtotime(hours($extratime));
$taxi = strtotime(hours($taxitime));
$flt = strtotime($flttime);
$res = strtotime(hours($options->restime));
$hold = strtotime(hours($holdtime));
$totalseconds = $extra + $taxi + $flt + $res + $hold;
return date("H.i", $midnight + $totalseconds);
}
$holdtime, $taxitime, $extratime, $options->restime are all written as minutes and then converted to hours using:
function hours($min)
{
$mins = abs($min);
$neg = ($min < 0) ? '-' : '' ;
$hours = $mins / 60;
$onlyhours = floor($hours);
$onlymins = $mins - ($onlyhours*60);
$time = sprintf("%s%d.%02d",$neg,$onlyhours,$onlymins);
return $time;
}
The function hours works fine but function add doesn't as after displaying it I get 11.03 where it should be 10.12.
Thanks in advance,
Maciej.
Do not convert $holdtime, $taxitime, $extratime, $options->restime in hours.
Add these durations in minutes :
$total = $holdtime + $taxitime + $extratime + $options->restime;
Then convert $total in hours.
Related
I want to get the difference in two times that are in hh:mm format. I am using this function
<?php
function timeDiff($firstTime,$lastTime) {
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
$timeDiff=$lastTime-$firstTime;
return $timeDiff;
}
echo (timeDiff("10:00","20:00")/60)/60;
?>
The issue is that it works perfectly if I have hours less than 24. But I need it to work for upto 60 hours. Like 60:00 - 02:25 should give me 57:35 and 60:00 - 00:00 should give me 60:00. Where are I doing wrong?
If you only work with hours + minutes, then you can just calculate the timestamp yourself, something like that:
function calculateSeconds($time) {
$timeParts = explode(':', $time);
return (int)$timeParts[0] * 3600 + (int)$timeParts[1] * 60;
}
and then use it in your function
function timeDiff($firstTime, $lastTime) {
return calculateSeconds($lastTime) - calculateSeconds($firstTime);
}
You can use gmdate() function as bellow.
function timeDiff($firstTime,$lastTime) {
$a_split = explode(":", $firstTime);
$b_split = explode(":", $lastTime);
$a_stamp = mktime($a_split[0], $a_split[1]);
$b_stamp = mktime($b_split[0], $b_split[1]);
if($a_stamp > $b_stamp)
{
$diff = $a_stamp - $b_stamp; //69600
}else{
$diff = $b_stamp - $a_stamp; //69600
}
$min = gmdate("i", $diff);
$d_hours = gmdate("d", $diff)==1 ? 0 : gmdate("d", $diff)*12 ;
$hours = $d_hours + gmdate("H", $diff) ;
echo $hours . ':' . $min; // 56:12:12
}
timeDiff("35:05", "01:45");
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 have an To make a sum of hours.
foreach ($horaires as $horaire) {
$addition += strtotime($horaire->getPointNbh()->format('H:m:s'));
}
And it s not working. Somebody can help me about how to make operations on DateTimne Type with Symfony 2.
Thanks
Without Solution in side of Symfony or Datetime object, this is my solution:
$heure = 0;
foreach ($horaires as $horaire) {
$heure += $horaire->getPointNbh()->format('H') *3600 + $horaire->getPointNbh()->format('i') *60 + $horaire->getPointNbh()->format('s'); ;
//$heure = $heure * 3600;
}
$hour = floor($heure /3600);
$min = floor($heure%3600/60);
$sec = $heure - (($hour*3600) + ($min*60));
$result = $hour.":".$min.":".$sec;
return $result;
//Difference between 2 dates
This function works well but display wrong time format. Pls how can I change the time of this function from GMT to GMT+1? Displays 15hrs 22mins instead of 16hrs 22mins.
Thanks
function get_date_diff($start, $end="NOW")
{
$sdate = strtotime($start);
$edate = strtotime($end);
$timeshift = "";
$time = $edate - $sdate;
if($time>=0 && $time<=59) {
// Seconds
$timeshift = $time.' seconds ';
} elseif($time>=60 && $time<=3599) {
// Minutes + Seconds
$pmin = ($edate - $sdate) / 60;
$premin = explode('.', $pmin);
$presec = $pmin-$premin[0];
$sec = $presec*60;
$timeshift = $premin[0].' min '.round($sec,0).' sec '."<b>ago</b>";
} elseif($time>=3600 && $time<=86399) {
// Hours + Minutes
$phour = ($edate - $sdate) / 3600;
$prehour = explode('.',$phour);
$premin = $phour-$prehour[0];
$min = explode('.',$premin*60);
$presec = '0.'.$min[1];
$sec = $presec*60;
$timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '."<b>ago</b>";
} elseif($time>=86400) {
// Days + Hours + Minutes
$pday = ($edate - $sdate) / 86400;
$preday = explode('.',$pday);
$phour = $pday-$preday[0];
$prehour = explode('.',$phour*24);
$premin = ($phour*24)-$prehour[0];
$min = explode('.',$premin*60);
$presec = '0.'.$min[1];
$sec = $presec*60;
$timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '."<b>ago</b>";
}
return $timeshift;
}
Set the timezone using the date_default_timezone_set() function. If that doesn't work, you have PHP <5.1.0 and should set the TZ environment variable. Check here for the detailed order of preference used to determine the timezone.
You could also skip all that code you wrote and use the date() function to get the resulting datetime string in pretty much any format you desire.
i have created work total time program in PHP - give input time - 1.30, 2.10, 1.40 and get output time - 4.80(8 hrs). but i need output time - 5.20(8.40 hrs).
Notes: 1.30+2.10+1.40=4.80(8 hrs), but i need 5.20(8.40 hrs). please help me...
1.30 + 2.10 + 1.40 is wrong. Should be:
((1 * 60) + 30) + ((2 * 60) + 10) + ((1 * 60) + 40) = 320 (minutes)
320 minutes = 5 hours and 20 minutes.
You need to keep track of minutes and seconds separately:
$minutes = array();
$seconds = array();
foreach ($times as $time) {
$parts = explode('.', $time);
$minutes[] = $time[0];
$seconds[] = $time[1];
}
$total_minutes = array_sum($minutes);
$total_seconds = array_sum($seconds);
while ($total_seconds > 60) {
$total_minutes++;
$total_seconds -= 60;
}
echo $total_minutes . ' minutes and ' . $total_seconds . ' seconds';
Excerpt from PHP site for your pleasure:
function AddTime ($oldTime, $TimeToAdd) {
$pieces = split(':', $oldTime);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$oldTime=$hours.":".$minutes.":".$seconds;
$pieces = split(':', $TimeToAdd);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$str = $minutes." minute ".$seconds." second" ;
$str = "01/01/2000 ".$oldTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ;
if (($timestamp = strtotime($str)) === false) {
return false;
} else {
$sum = date('h:i:s', $timestamp);
$pieces = split(':', $sum);
$hours = $pieces[0];
$hours = str_replace("12", "00", $hours);
$minutes = $pieces[1];
$seconds = $pieces[2];
$sum = $hours.":".$minutes.":".$seconds;
return $sum;
}
}
$firstTime = "00:03:12";
$secondTime = "02:04:34";
$sum=AddTime($firstTime, $secondTime);
if($sum != false) {
echo $firstTime." + ".$secondTime." = ".$sum;
} else {
echo "failed";
}
Output:
00:03:12 + 02:04:34 = 02:07:46
For each number (represented as $t below) you can do this:
// start with $total=0
$hours = floor($t); // 1.10 -> 1 hr
$minutes = ($t - $hours) * 100; // 1.10 -> 10 mins
$total += ($hours * 60) + $minutes;
That gives you the total number of minutes. To get hours/mins separately, do this:
$total_mins = $total % 60; // 130 -> 10 mins
$total_hours = ($total - $total_mins) / 60; // 130 -> 2 hrs