This question already has answers here:
How to get millisecond between two dateTime obj?
(5 answers)
Closed 5 years ago.
For example:
$date1='2017-12-13 13:06:21.602';
$date2='2017-12-13 13:06:18.102';
$diff=$date1-$date2
echo $diff;
output should be 3.5
Use The DateInterval class
Note: This solution works since PHP 7.1.0 when was added parameter f.
// your code goes here
$date1 = new DateTime('2017-12-13 13:06:21.602');
$date2 = new DateTime('2017-12-13 13:06:18.102');
$interval = $date1->diff($date2);
echo $interval->s+$interval->f;
// s = seconds, f = microseconds, as a fraction of a second
//var_dump($interval);
The result is 3.5
https://ideone.com/kf1PQQ
For PHP version comparison since 4.3.0: https://3v4l.org/VCI6P
Here is a solution 5.2.2 < php version < 7.1
<?php
$date1=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:21.602');
$date2=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:18.102');
// $di = $date1->diff($date2);
// var_dump( $di );
// seconds
$diffSeconds = $date1->getTimestamp() - $date2->getTimestamp();
// milli
$diffMilli = ($date1->format('u') - $date2->format('u')) / 1000000;
//result :
$diffWithMilli = $diffSeconds + $diffMilli;
var_dump($diffWithMilli);
Related
This question already has answers here:
PHP - time minus time to minutes
(2 answers)
php - One Hour Earlier Than Given Datetime
(5 answers)
Closed 5 years ago.
I have a string time $a = "17:20:00";
I want to do
if($a > "13:00:00"){
$c = $a - "01:00:00";
}
I want to get $c = "16:20:00"
How can this be done?
Thank you!
$a is a string and can't be compared like that.
Use strtotime to make it UNIX time (integer) and subtract 3600 seconds.
And then use date to convert back to string.
$a = strtotime("17:20:00");
if($a > strtotime("13:00:00")){
$c = $a - 3600;
}
Echo date("H:i", $c);
https://3v4l.org/fOggP
You should try this:
if(strtotime($a) > strtotime("13:00:00")){
$c = date( "h:i:s", strtotime($a) - strtotime("01:00:00"));
}
You should try this!!!
This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I get the string data from the database
The data format like:
07:00:00.0000000
It means 7:00 am.
How can i convert this string into Date type for comparison.
Therefore, I can get 2 data. Such as
Start:
07:00:00.0000000
End:
16:30:00.0000000
After the time comparison, i can got a answer like 9.3.
You can use strtotime :
$start = strtotime("07:00:00.0000000");
$end = strtotime("16:30:00.0000000");
$difference = $end - $start;
Or with DataTime object :
$start = new DateTime("07:00:00.0000000");
$end = new DateTime("16:30:00.0000000");
$interval = $start->diff($end);
$difference = $end->getTimestamp() - $start->getTimestamp();
Then echo the result :
echo $difference; // in seconds
echo $difference / 60; // in minutes
echo $difference / 3600; // in hours
echo $interval->format('%s') // in seconds
It's then up to your preference. I also suggest you to have a look at this post regarding the performances of the two solutions.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
I have two dates
$departure_Dtae=2012-07-25T07:30:00
$arrival_date =2012-07-25T10:25:00
T means Time i.e. in 2012-07-25T10:25:00 date is 2012-07-25 and the time is 10:25:00 (hr: mts: s)
I need to find out the total hrs between these two times
I.e. in this case the total hour is 2 hr and 55 minutes
But I don't know how I calculate this in PHP
Does anyone know this?
If using PHP Version 5.3+ then you can use DateTime::diff():
<?php
function getDiff($strStart, $strEnd) {
$start = DateTime::createFromFormat("Y-m-d G:i:s", $strStart);
$end = DateTime::createFromFormat("Y-m-d G:i:s", $strEnd);
return $start->diff($end)->format('%hhrs %imins and %sseconds ');
}
var_dump(getDiff('2012-07-25 07:30:00', '2012-07-25 10:25:00'));
its simple
$departure_Dtae="2012-07-25T07:30:00";
$arrival_date ="2012-07-25T10:25:00";
$departure_Dtae= str_replace("T", " ", $departure_Dtae);
$arrival_date= str_replace("T", " ", $arrival_date);
$diff= (strtotime($arrival_date)-strtotime($departure_Dtae));
echo date("h",$diff);
Try this :
$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";
$end = date("h:i:s",strtotime($end_time));
$start = date("h:i:s",strtotime($start_time));
$diff = strtotime($end) - strtotime($start);
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds
//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes
$total_visit = $convert_hr.":".$remainder;
You can use the DateTime object in php, which has loads of methods for manipulating dates.
DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php and DateTime::diff http://www.php.net/manual/en/datetime.diff.php would be the functions you would need to perform this task.
$date1 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 12:45');
$date2 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 13:45');
$interval = $date1->diff($date2);
echo $interval->format('H:i');
strtotime(date($arrival_date)) - strtotime(date($departure_date));
Will give you the time diff in secounds, then you can manipulate that as you wish.