add time result not correct in PHP - php

I have problem with adding time which is not correct.
Example:
00:00 + 00:35 = 11:13
it should be 00:35 instead of 11:13
This is my code for above:
echo date('H:i',$total).' + '.$telat2.' = ';
if($telat2 == '00:00'){
$total = $total;}
else{
$total = ($total) + strtotime($telat2);}
echo date('H:i',$total).' ';
I hope everybode=y here could help me..
Thanks in advance..
Update 1
I have just got the right code!
This is the code:
$total_unix = strtotime(date('Y-m-d').' '.$total.':00');
$telat2_unix = strtotime(date('Y-m-d').' '.$telat2.':00');
$begin_day_unix = strtotime(date('Y-m-d').' 00:00:00');
$total = date('H:i', ($total_unix + ($telat2_unix - $begin_day_unix)));
I wonder how can this be happen?
Can anybody explain this to me?

Made a working code for original guestion:
<?php
function AddTime($telat1,$telat2)
{
$telat1 = date_parse_from_format("H:i", $telat1);
$telat2 = $timeToAdd;
$telat2 = date_parse_from_format("H:i", $telat2);
$totalHours = $telat1['hour'] + $telat2['hour'];
$totalMinutes = $telat1['minute'] + $telat2['minute'];
if($totalMinutes >= 60)
{
$totalHours += 1;
$totalMinutes -= 60;
}
if($totalHours >= 24)
{
$totalHours -= 24;
}
// Setting the leading zeros
if(strlen($totalMinutes) < 2)
{
$totalMinutes = "0" . $totalMinutes;
}
if(strlen($totalHours) < 2)
{
$totalHours = "0" . $totalHours;
}
$total = $totalHours . ":" . $totalMinutes;
return $total;
}
$timeNow = date('H:i'); // Base time
$timeToAdd = '12:12'; // 12 hours 12 minutes to be added to $timeNow
$total = AddTime($timeNow, $timeToAdd);
echo $total;
?>

Related

How do suptract 33 hours - 30 min in PHP [duplicate]

This question already has answers here:
Calculate difference (in hours) between two times in PHP
(2 answers)
Closed 4 months ago.
My Code is :
$hm = strtotime('33:00:00')-strtotime('00:30:00');
$hms = gmdate("H:i:s", $hm);
i need the output is 32:30:00
Use this code to get subtracted values.
$time1 = '33:00:00';
$time2 = '00:00:00';
echo 'Subtract = '.subtractTwoTimes($time1, $time2);
function subtractTwoTimes($time1, $time2){
$time1_parts = explode(':', $time1);
$time1_hours = $time1_parts[0];
$time1_mins = $time1_parts[1];
$time1_secs = $time1_parts[2];
$time2_parts = explode(':', $time2);
$time2_hours = $time2_parts[0];
$time2_mins = $time2_parts[1];
$time2_secs = $time2_parts[2];
$total_hours = $time1_hours - $time2_hours;
$total_mins = $time1_mins - $time2_mins;
$total_secs = $time1_secs - $time2_secs;
if($total_secs < 0){
$total_secs += 60;
$total_mins--;
}
if($total_mins < 0){
$total_mins += 60;
$total_hours --;
}
return $total_hours.':'.$total_mins.':'.$total_secs;
}
To get added or subtracted value from same function.
This is an edited answer. I optimized the code to make it work for you in one function.
$time1 = '33:00:00';
$time2 = '00:30:00';
echo 'Add = '. addOrSubtractTwoTimes($time1, $time2, 'add');
echo '<br>';
echo 'Subtract = '. addOrSubtractTwoTimes($time1, $time2, 'subtract');
function addOrSubtractTwoTimes($time1, $time2, $operation){
$time1_parts = explode(':', $time1);
$time1_hours = $time1_parts[0];
$time1_mins = $time1_parts[1];
$time1_secs = $time1_parts[2];
$time2_parts = explode(':', $time2);
$time2_hours = $time2_parts[0];
$time2_mins = $time2_parts[1];
$time2_secs = $time2_parts[2];
switch ($operation){
case 'add':
$total_hours = $time1_hours + $time2_hours;
$total_mins = $time1_mins + $time2_mins;
$total_secs = $time1_secs + $time2_secs;
if($total_secs > 59){
$extra_mins = floor($total_secs/60);
$total_secs = $total_secs%60;
$total_mins += $extra_mins;
}
if($total_mins > 59){
$extra_hours = floor($total_mins/60);
$total_mins = $total_mins%60;
$total_hours += $extra_hours;
}
break;
case 'subtract':
$total_hours = $time1_hours - $time2_hours;
$total_mins = $time1_mins - $time2_mins;
$total_secs = $time1_secs - $time2_secs;
if($total_secs < 0){
$total_secs += 60;
$total_mins--;
}
if($total_mins < 0){
$total_mins += 60;
$total_hours --;
}
break;
}
return sprintf('%02d', $total_hours).':'.sprintf('%02d', $total_mins).':'.sprintf('%02d', $total_secs);
}

Sum of hours in multiple dates

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;

My php or Mysqli time / timestamps seem out?

The code is
$la=time()-$row['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
print "<li><a class=\"button\" href=\"user.php?p={$row['uid']]'}\">{$row['display_name']} ~($la $unit)</a></li>";
and $row['laston']; is stored like: 1392490566
My problem is it is showing like:
User1 ~(-58secs) when i refresh the page, I'd expect it to be 0-2 secs not a negative value?
Could someone please help me understand what is causing this?
and would changing $la=time()-$row['laston']; to $la=time()-$row['laston']+58; be an appropriate fix?
Try this:
$la = time() - $row['laston'];
if($la > 0) {
$newLa = $la / 24;
$unit = "days";
} if($la < 84600) {
$newLa = $la / 60;
$unit = "hours";
} if($la < 3600) {
$newLa = $la / 60;
$unit = "mins";
} if($la < 60) {
$newLa = $la;
$unit = "secs";
}
echo round($newLa, 2) . ' ' . $unit;

PHP Convert from strtotime into time

I need to add multiple time values as in Hours:mins, so I use
strtotime($value1) + strtotime($value2)
to add all of them, how do I put them back as hours:mins ?
cant use
date("h:i")
it only works if hours < 24.
I appreciate your help. Thanks
Here is an function that will sum all your time values in format HH:MM:
function sum_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 sum_time('01:05', '00:02', '05:59'); # 07:06
demo
Try this :
function time_convert($s) {
$m = 0; $hr = 0; $td = "now";
if ($s > 59) {
$m = (int)($s/60);
$s = $s-($m*60); // sec left over
$td = "$m min";
}
if ($m > 59) {
$hr = (int)($m / 60);
$m = $m - ($hr*60); // min left over
$td = "$hr hr";
if ($hr > 1) {
$td .= "s";
}
if ($m > 0) {
$td .= ", $m min";
}
}
return $td;
}
And use it:
$time = (int) strtotime($v1) + strtotime($v2);
echo time_convert($time);
May it helps
The function strtotime() returns the time in seconds since January 1 1970 00:00:00 UTC. So adding the return value of this function might not do what you would expect.
Instead of using the date functions we can manipulate the string and perform some basic arithmetic operations:
<?php
$value1 = "12:44";
$value2 = "13:47";
$arr1 = explode(':', $value1);
$arr2 = explode(':', $value2);
$totalMinutes = (int)$arr1[0] * 60 + (int)$arr1[1] + (int)$arr2[0] * 60 + (int)$arr2[1];
$hours = (int) ($totalMinutes / 60);
$minutes = $totalMinutes % 60; // Modulus: remainder when dividing with 60
echo $hours . ':' . $minutes;
?>
Another way with DateTime
$dt1 = new DateTime($value1);
$dt2 = new DateTime($value2);
$interval = $dt1->diff($dt2);
echo $interval->format('%a day(s) %h hour(s) %i minute(s)') . '<br />';
echo ($interval->format('%a') * 24 + $interval->format('%h')) . ' hour(s) ';
echo $interval->format('%i minute(s)');

how get php total time?

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

Categories