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');
Related
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
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449
This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
Closed 6 years ago.
My question is:
I have two dates in string format:
string(10) "2016-21-10"
string(10) "2016-05-10"
I can not figure out how to compare them. So basically i need the difference between the two given date. How can I achieve it?
You can use createFromFormat() function to create DateTime object from string.
Create two DateTime object like this,
$date1 = DateTime::createFromFormat('Y-d-m', '2016-21-10');
$date2 = DateTime::createFromFormat('Y-d-m', '2016-05-10');
To get the difference between two dates you can use diff() function. Use it like this,
$interval = $date1->diff($date2);
You can use the $interval variable to get the difference between two dates.
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 9 years ago.
I have a string "2013-10-09 00:00:00" and I use the code below to change it to a timestamp
date_default_timezone_set($timeZone);
$timeStamp = strtotime("2013-10-09 00:00:00"); //echos 1381269600
When I do
date_default_timezone_set($timeZone);
date("Y-m-d H:m",$timeStamp);
I get 2013-10-09 00:10:00. This is completely strange. Why do I get this 10 minutes difference?
Because you are using the m which is for the month not for the minute. You need to use i.
See http://php.net/manual/en/function.date.php
Your code should be
date("Y-m-d H:i", $timeStamp);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);