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
Related
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
This question already has answers here:
Convert timestamp to readable date/time PHP
(14 answers)
Closed 3 years ago.
i am using sports api and i am getting matches schedule from api but the timestamp is UTC and i want to search match according to my localdate the timestamp is look like this 1574463600
You can use # along with the unix time to create a DateTime class.
<?php
$unixTime = 1574463600;
$date = new DateTime('#' . $unixTime);
echo $date->format('d/m/Y'); // outputs 22/11/2019
See it here https://3v4l.org/eoSU9
Read about DateTime here https://www.php.net/manual/en/class.datetime
This question already has answers here:
Laravel Carbon date diffInDays() on string error
(7 answers)
Closed 3 years ago.
Hello I want to get days of between two different dates.
$expire_date = ("2019-07-22")
$current_date = Carbon::now()->format('Y-m-d');
$interval = $expire_date->diffIndays($current_date)->days;
dd($interval);
display this error:
Call to a member function diffIndays() on string
Firstly, you'll need to make your $expire_date into a Carbon instance:
$expire_date = Carbon::parse("2019-07-22");
Then diffInDays() already returns the days so you just need to remove ->days. Lastly, don't convert the current date into a string:
$interval = $expire_date->diffIndays(now());
This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
time difference in HH:MM:SS format
(2 answers)
Closed 5 years ago.
I am trying to find the time difference between two time values in the format H:mm:ss using PHP.
When I tried with the following code, I'm getting the difference 01:00:20 instead of 00:00:20. What's wrong with my code?
$start_time = strtotime("0:17:14");
$end_time = strtotime("0:17:34");
$diff = $end_time - $start_time;
echo date('H:i:s', $diff);
Your $diff variable is not a timestamp, it's a duration/interval. The date() function is intended to format timestamps, and won't properly handle intervals like you're expecting.
Instead, try using the DateTime class to read your timestamps, and turn the difference between them into a DateInterval using DateTime::diff(). You can then use DateInterval::format to get the output you want.
Something like this should work:
$start_time = new DateTime("0:17:14");
$end_time = new DateTime("0:17:34");
$diff = $end_time->diff($start_time);
echo $diff->format('%H:%I:%S');
This question already has answers here:
How to add 5 minutes to current datetime on php < 5.3
(7 answers)
Closed 8 years ago.
I have a mysql table which contains a field named time and has the "time" format. I added the first line into the table manually and its time is "14:55:00".
Now, in my PHP page, users will fill a form and then submit it. Here, I want my php to assign the last register's time value into a variable like $time (i can do that). Then I want this:
$time=$time + 5 minutes
How can I do that?
If you want to update your sql table, you can use the DATE_ADD function:
UPDATE table SET time = DATE_ADD(time, INTERVAL 5 MINUTE)
If you want to add 5 minutes to a timestamp in PHP you can use the function strtotime:
$time = $time + strtotime("+5 minutes");
If your time is a string (ie in format 14:55:00), you can do the following:
$timeAsString = "14:55:00";
$timestamp = strtotime("+5 minutes", strtotime($timeAsString));
$time = date("H:i:s", $timestamp);
echo $time;
$dateMinutes= date("i")+5;
and use the make time function from php