How to calculate time difference? - 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

Related

PHP display in minute only

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);

PHP - calculate current minute of a soccer match

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.

add $interval1 and interval2 in time format

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);

Timestamp older than current time if else statement

Im testing an online/offline function with timestamp from the database against DateTime NOW.
But I cant figure out how to set interval time on lets say 5 minutes.
If the user has not done anything in 5 minutes, appear offline.
DateTime NOW will always be bigger than Database time.
Any ideas on how to set the interval time on the statement ?
This is what i have so far,
// $user_info['activity'] 2013-04-27 19:27:39 //
$dateTime = new DateTime("now", new DateTimeZone('Europe/Warsaw'));
if($user_info['activity']>$dateTime){
echo 'user Online';
}else{
echo 'user inactive';
}
Something liKe this might work
$now = new DateTime(null, new DateTimeZone('Europe/Warsaw'));
$last_activity = new DateTime($user_info['activity']);
$last_activity->modify('+5 minutes');
if($now > $last_activity){
echo 'user inactive';
}else{
echo 'user online';
}
Use DateTime::diff() for this:
$now = new DateTime();
$activity = $user_info['activity'];
// get difference between the two dates
$difference = $now->diff($activity);
// ->i contains the minutes
if($difference->i >= 5) {
echo "logged out";
}
One possible solution is to change your database timestamp to be a DateTime object as well and then use the DateTime::diff() method for comparison.
$dateTime1 = new DateTime(strtotime($user_info['activity']), new DateTimeZone('Europe/Warsaw'));
$dateTime2 = new DateTime("now", new DateTimeZone('Europe/Warsaw'));
$interval = $dateTime1->diff($dateTime2);
This will allow you to determine the interval and do a comparison using it.
I did this recently, like this:
$time = time();
$last_activity = //time from db
$time_since = $time - $last_activity
Then clean up $time_since if necessary for your application.

Getting date/time difference in PHP

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.

Categories