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.
Related
I'm working on a tick based space game http://ricarion.com/ but the ticks only run between certain hours.
08:00-16:30 - run every 30 minutes via a cron job. In the nav bar at the top I want to add "Next Tick: 08:30 06/02/20" for example.
So I was thinking of creating an array:
$tick_times[] = array();
$tick_times[] = 08:00;
$tick_times[] = 08:30;
$tick_times[] = 09:00;
...
$tick_times[] = 16:30;
And then this is where I get stuck, how do I check the existing time, and then compare that against the array selecting the next future time? i.e. It's now 08:34, so the return should be 09:00?
Did you need an array or just want to calculate the next 30-minute interval?
If so this may be similar to:
Round minute down to nearest quarter hour
You do modulo division of 1800 seconds on the current time and add the answer (time remainder of time to the next interval) to the then-current time to get the next event.
<?php
$current_date = date('d-M-Y g:i:s A');
echo $current_date."\n";
$current_time = strtotime($current_date);
$frac = 1800;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);
echo $new_date."\n";
http://codepad.org/xs9lMCRQ
Get the now time format it and compare it. In your case you maybe format your $tick_time to the same format like current time.
$date = new DateTime('now');
$date = $date->format('Y-m-d H:i:s');
foreach ($tick_times as $tick_time) {
$date_added = new DateTime($tick_time);
if (strtotime($date_added) == strtotime($date)) {
//do your stuff here
}
}
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 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 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);
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.