i apologize for the last time i didn't questioned properly.it was my first question raised in stackflow.please help me to add $interval1 and interval2 in time format ..i hav spend my whole day trying this .thank u ..
public function calculate_time_lap() {
$formate = "%h:%i";
$time1 = $this->input->post('time1');
$time2 = $this->input->post('time2');
$time3 = $this->input->post('time3');
$time4 = $this->input->post('time4');
$datetime1 = date_create($time1);
$datetime2 = date_create($time2);
$datetime3 = date_create($time3);
$datetime4 = date_create($time4);
$interval1 = date_diff ( $datetime2, $ datetime1);
$interval2 = date_diff( $datetime4, $datetime3);
add// ( $interval=($interval1,$interval2));
echo $interval->format($formate);
}
two add two time together you need to convert them into seconds
$date1='3:20:10';
$date2='14:25:10';
$tmstamp = strtotime($date2) - strtotime($date1);
echo gmdate("H:i:s", $tmstamp);
Related
Sorry for my bad english, im trying to convert duration from given date time range in minute only. There something weird when display only %I. My final out put using variable $durationDisplayMin. How how convert in minute only?
$startDate = "2018-01-20 15:10:10";
$end_datetime = "2018-07-29 11:11:05";
$start_datetime = new DateTime($startDate);
$end_datetime = new DateTime($endDate);
$diffr = $start_datetime->diff($end_datetime);
$durationDisplayMin = $diffr->format("%I");
just found the answer:
$durationInMin = 0;
$start_datetime = new DateTime("2018-01-20 15:10:10");
$end_datetime = new DateTime("2018-07-29 11:11:05");
$Date1 = strtotime($start_datetime->format('Y-m-d H:i:s'));
$Date2 = strtotime($end_datetime->format('Y-m-d H:i:s'));
$durationInMin += round(abs($Date1 - $Date2)/ 60,2);
I have two dates that are in format Y-m-d
$dateOld = new DateTime("2017-01-10");
$dateNew = new DateTime("2017-01-11");
echo $diff = $dateNew->diff($dateOld)->format("%a");
this is working perfect and giving me exact days left.
But now I have added time and it is in H-M format
Like 23:38 and 17:21 and cannot understand now to get the difference between two dateTime
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-11 17:21");
echo $diff = $dateNew->diff($dateOld)->format("%a");
I want to get the difference even if the value if in floating point. Now to work with date concatenated with time?
Use this:
<?php
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-11 17:21");
$diff = $dateNew->diff($dateOld);
$days = $diff->d;
$hours = $diff->h;
$minutes = $diff->i;
$total_difference = $days + ($hours * 60 + $minutes) / 1440;
echo $total_difference;
Or, without the DateInterval:
$dateOld = new DateTime("2017-01-10 23:38");
$dateNew = new DateTime("2017-01-12 17:21");
$difference_in_seconds = $dateNew->getTimestamp() - $dateOld->getTimestamp();
$total_difference_in_days = $difference_in_seconds / 86400;
echo $total_difference_in_days;
Using ->format("%a") will give you the rounded days.
See http://php.net/manual/en/datetime.diff.php.
$dateNew = '2017-01-11 17:21';
$dateOld = '2017-01-10 23:38';
$dateNew = new DateTime($dateNew);
$dateOld = new DateTime($dateOld);
$diff = $dateNew->diff($dateOld);
echo $diff->format("%H:%I");
Source: http://php.net/manual/en/datetime.diff.php
i have 2 type of time values are 2015-09-25 11:52:22 and 2015-09-25 01:06:57. i using date different function in php. my code as
$date_a = new DateTime($from_time);
$date_b = new DateTime($to_time);
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');
Out put gives 10:45:25.how can calculated time diff??
2015-09-25 11:52:22 is AM and 2015-09-25 01:06:57 is PM that time only i got look like otherwise coming correct
The 'diff' method applies to a DateTime Object..
$from_time = '2015-09-25 11:52:22';
$to_time = '2015-09-25 01:06:57';
$date_a = new DateTime($from_time);
$date_b = new DateTime($to_time);
//$interval = date_diff($date_a,$date_b);
$interval = $date_a->diff($date_b);
echo $interval->format('%h:%i:%s');
// 10:45:25
I need some help to calculate the current minutes:seconds played in a soccer match, let's suppose a soccer match have :
$schedule='2014-02-13';
$start_time1='03:00 pm';
$end_time1='03:45 pm';
$start_time2='04:00 pm';
$end_time2='04:50 pm';
$start_extra_time1='05:10 pm';
$end_extra_time1='05:30 pm';
$start_extra_time2='05:35 pm';
$end_extra_time2='06:10 pm';
And I actually my current time is "05:32 pm", how I can calculate the current played minutes:seconds or (hours:minutes:second) of the match?
I have tried many solutions, like datediff, strtotime etc... but no luck, since there is some OFF time between these dates.
Thanks so much for helping.
Here's how to do it using DateTime() and DateInterval():
<?php
$schedule='2014-02-13';
$start_time1='03:00 pm';
$end_time1='03:45 pm';
$start_time2='04:00 pm';
$end_time2='04:50 pm';
$start_extra_time1='05:10 pm';
$end_extra_time1='05:30 pm';
$start_extra_time2='05:35 pm';
$end_extra_time2='06:10 pm';
$start1 = new DateTime($start_time1);
$end1 = new DateTime($end_time1);
$firstInterval = $start1->diff($end1);
$start2 = new DateTime($start_time2);
$end2 = new DateTime($end_time2);
$secondInterval = $start2->diff($end2);
$start3 = new DateTime($start_extra_time1);
$end3 = new DateTime($end_extra_time1);
$thirdInterval = $start3->diff($end3);
$start4 = new DateTime($start_extra_time2);
$end4 = new DateTime($end_extra_time2);
$fourthInterval = $start4->diff($end4);
$baseline = new DateTime();
$accrual = clone $baseline;
$accrual->add($firstInterval);
$accrual->add($secondInterval);
$accrual->add($thirdInterval);
$accrual->add($fourthInterval);
$difference = $accrual->diff($baseline);
echo $difference->format('%h hours, %i minutes');
See it in action
All this code does is:
create DateTime() objects representing the start and end time of each time interval.
get a DateInterval() object representing the difference between the two.
create a baseline (beginning point) DateTime() object
create a DateTime() object that we add our intervals to to represent time elapsed
echo out the difference between the two
You can shorten some of this if you use PHP 5.4 or newer:
$firstInterval = (new DateTime($start_time1))->diff((new DateTime($end_time1)));
$secondInterval = (new DateTime($start_time2))->diff((new DateTime($end_time2)));
$thirdInterval = (new DateTime($start_extra_time1))->diff((new DateTime($end_extra_time1)));
$fourthInterval = (new DateTime($start_extra_time2))->diff((new DateTime($end_extra_time2)));
and
$accrual->add($firstInterval)->add($secondInterval)->add($thirdInterval)->add($fourthInterval);
I have added the solution of John, here is my code:
<?php
date_default_timezone_set("Canada/Eastern");
$date="2014-02-14 14:00:00 +00";
$start_time1='09:00 am';
$end_time1='09:45 am';
$start_time2='10:00 am';
$end_time2='10:50 am';
$start_extra_time1='';
$end_extra_time1='';
$start_extra_time2='';
$end_extra_time2='';
$start1 = new DateTime($start_time1);
$end1 = new DateTime($end_time1);
$firstInterval = $start1->diff($end1);
$start2 = new DateTime($start_time2);
$end2 = new DateTime($end_time2);
$secondInterval = $start2->diff($end2);
$start3 = new DateTime($start_extra_time1);
$end3 = new DateTime($end_extra_time1);
$thirdInterval = $start3->diff($end3);
$start4 = new DateTime($start_extra_time2);
$end4 = new DateTime($end_extra_time2);
$fourthInterval = $start4->diff($end4);
$baseline = new DateTime($date);
$baseline->setTimezone(new DateTimeZone('Canada/Eastern')); // -05
$accrual = new DateTime();
$accrual->add($firstInterval);
$accrual->add($secondInterval);
$accrual->add($thirdInterval);
$accrual->add($fourthInterval);
$difference = $accrual->diff($baseline);
echo "baseline time :\n";
var_dump($baseline);
echo "accrual time :\n";
var_dump($accrual);
print json_encode(array('response'=>$difference->format('%h hours, %i minutes, %s seconds')));
?>
The game not started yet, but it show 1 hour passed in the game, how it's possible? http://easycaptures.com/fs/uploaded/645/4891434894.png
Mysql has a bug with timediff, so use this:
Time_To_Sec($currentime) - Time_To_Sec($end_time2) As time_seconds
Also check your time format, avoid the use of strings as time or dates, format as date/time entities.
Am trying to get the time difference between two days. But for certain date/time, I get wrong answers
Here is my code:
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
The awnser is correct. You provide two times. Without a date there is no way to know the last date is actually the next day. Just because you named the variable "end_date" doesnt mean PHP knows what you mean.
Perhaps you should include the date aswell in your request like
$start_date = new DateTime('2012-12-07 23:58:40');
$end_date = new DateTime('2012-12-08 00:11:36');
If you realy want to work with just times:
function differenceInTimes($start, $end) {
if (strtotime($start)>strtotime($end)) {
//start date is later then end date
//end date is next day
$s = new DateTime('2000-01-01 '.$start);
$e = new DateTime('2000-01-02 '.$end);
} else {
//start date is earlier then end date
//same day
$s = new DateTime('2000-01-01 '.$start);
$e = new DateTime('2000-01-01 '.$end);
}
return date_diff($s, $e);
}
$start_date = '23:58:40';
$end_date = '00:11:36';
$dd = differenceInTimes($start_date, $end_date);
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
//Hours = 0, Minutes = 12, Seconds = 56
Swap the arguments to date_diff
$dd = date_diff($start_date, $end_date);
Edit
After actually testing this theory it proved to be totally useless, giving the same answer.