Time difference calculate late - PHP [duplicate] - php

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
what I want to do is calculate the latein these time:
Time-in = 22:00 (10:00 PM)
Time-out = 1:30 (1:30 AM)
The output should be 3:30 i want to get this output
How can I calculate the difference and what built-in function in php should be used to achieved this? Can someone give me a clue?

Assuming your times are strings in the form HH:mm, this will work:
$in = new DateTime('22:00');
$out = new DateTime('01:30');
// if in is > out, assume out is on the next day
if ($in > $out) $out->add(new DateInterval('P1D'));
$diff = $out->diff($in);
echo $diff->format('%H:%i');

Related

How to get time in between start and end time in php? [duplicate]

This question already has answers here:
Getting time difference between two times in PHP [duplicate]
(3 answers)
Closed 2 years ago.
$time1 = new DateTime($post->clock_in);
$time2 = new DateTime($post->clock_out);
$interval = date_diff($time1,$time2);
$difference = $interval->format('%H:%I');
In this above code I want to get exact time between two different time. Now, What happen if $time1 = '10:00' and $time2 = '05:00' then difference between these two time is 07:00 but it show 05:00. I don't know why? So, How can I get the exact timing between two timing? Please help me.
Thank You
use
$d1=new DateTime("2012-07-08 11:14:15.638276");
$d2=new DateTime("2012-07-08 11:14:15.889342");
$diff=$d2->diff($d1);
print_r( $diff ) ;
https://www.php.net/manual/en/class.dateinterval.php

How to find time difference in Hours Minutes and seconds in PHP [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 3 years ago.
I want to calculate the time difference in Hours, Minutes and seconds between two-time intervals.
Here is my Code
$actualLoginTime = strtotime('13:49:13.0000000');
$loginTime = strtotime('09:00:00.0000000');
$nInterval = ($actualLoginTime) - ($loginTime);
{{date("H:i:s ", strtotime($nInterval))}}
It's giving me the wrong Output and showing the difference 5:00:00.
Can you guys please help me.
I found the solution to my Question by using gmdate() function:
echo gmdate("H:i:s", $nInterval );

compare two different dates in php [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 3 years ago.
I have 2 dates. One is $monthToCheck = date('Y-m-01'); and the other one comes from an API and its stored in a variable : $reqs['end_date']. My question is : How do i check if the date from the api is bigger than $monthToCheck. With bigger i mean for example 2020 - 01 - 01 is bigger than 2019 - 12 - 01 because the year is higher.
What i tried :
$time = strtotime($reqs['end_date']);
$monthToCheck = date('Y-m-01');
if($time > $monthToCheck) {...}
I converted the date from the API to a date using strtotime and then compared it with my $monthToCheck variable.But i have a feeling something isnt right since the results are all "true" eventho that is not always the case.
Just put your $monthToCheck into strtotime():
$monthToCheck = strtotime(date('Y-m-01'));
Example
php can compare dates formatted year-mm-dd easily
if($monthToCheck > $reqs['end_date']){
// do something
}

Difference between two timestamp in hh:mm:ss format [duplicate]

This question already has answers here:
calculate the difference between 2 timestamps in php
(7 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I have two timestamp like below format in my database.
$time1 = 4/19/2019 12:21:01 AM
$time2 = 4/19/2019 12:22:50 AM
I want get difference between this two timestamp like
12:21:01
Let me know if someone can give me idea/solution for do it using php.
You can get the difference as
$time1 = '4/19/2019 12:21:01 AM';
$time2 = '4/19/2019 8:15:01 PM';
$start = new DateTime($time1);
$end = new DateTime($time2);
$diff = $start->diff($end);
print $diff->format("%H:%I:%S");
For more details PHP Manual
So you should have mysql select query like this:
Select time1,time2,time_format(abs(timediff(time1,time2)), "%H:%i") as diff From table1
You can change time format, follow this link for time_format function

PHP: Time difference in seconds [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I would like to get the total number of seconds based from the given time, for example the following given data below:
$timein=04-01-2017 7:56:37 am;
$timeout=04-01-2017 5:15:17 pm;
$totalseconds= ?
PHP code demo
<?php
$time1=strtotime("04-01-2017 7:56:37 am");
$time2=strtotime("04-01-2017 5:15:17 pm");
echo $time2-$time1;

Categories