Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate the different between two datetime on seconds?

i want to calculate different between two datetime in seconds and check if the result > 300sec

$d1 = new DateTime("2016-03-25 19:29:21");
$d2 = new DateTime(date('Y-m-d H:i:s'));
like image 304
hamzanatek Avatar asked Mar 29 '16 10:03

hamzanatek


2 Answers

please check following code:

$datetime1 = new DateTime();
$datetime2 = new DateTime('2016-03-25 19:29:21');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;
like image 63
Gaurav Jariwala Avatar answered Oct 06 '22 15:10

Gaurav Jariwala


check this

if($d2->format('U')-$d1->format('U')>300){
    // greater than 300
}else{
    // less than 300
}
like image 42
Nooh Avatar answered Oct 06 '22 17:10

Nooh