Calculating Time Difference in PHP
Many applications require the calculation of time differences, such as determining late employee attendance. In PHP, this can be achieved using the strtotime() function.
In the example provided, the constant check time is set to 09:00:59, representing the expected login time. To calculate the time difference for a specific login time, follow these steps:
$checkTime = strtotime('09:00:59'); $loginTime = strtotime('09:01:00');
$diff = $checkTime - $loginTime;
if ($diff < 0) { echo 'Late!'; } else { echo 'Right time!'; }
echo 'Time diff in hours: ' . abs($diff) / 3600; echo 'Time diff in minutes: ' . abs($diff) / 60; echo 'Time diff in seconds: ' . abs($diff);
By using this approach, you can effectively calculate the time difference between two times and determine whether the login time is within the acceptable range, late, or very late.
The above is the detailed content of How to Calculate Time Differences in PHP: Late or On Time?. For more information, please follow other related articles on the PHP Chinese website!