Calculating time difference in php

Here is a simple screnario where in we need to calculate difference in time..

<?php
$a = date('H:i:s');
usleep(2000000);
$b = date('H:i:s');
$a_unix = strtotime($a);
$b_unix = strtotime($b);
$time_diff = $b_unix - $a_unix;
echo $time_diff;
?>

Date function is used to get current date and time , based on the options.
H – denotes Hours in 24 hour format (Note : ‘h’ denotes hour in 12 hour format)
i – denotes number of minutes
s – denotes number of seconds

strtotime converts the time to unix timestamps. finding the difference gives the output in seconds. For example, in the previous code we set delay as 2 seconds. Hence the output will be 2.

Advertisements

Comments are closed.