Determining Time Elapsed Since a DateTime Stamp in PHP
In PHP, obtaining the time passed since a specific date and time stamp is crucial. This information can be useful in displaying elapsed time in a user-friendly format, such as "xx Minutes Ago" or "xx Days Ago."
Solution:
The provided code exemplifies an effective approach to convert a date and time stamp into a relative time format:
<?php $timestamp = strtotime('2010-04-28 17:25:43'); function humanTiming($timestamp) { $difference = time() - $timestamp; $tokens = array( 31536000 => 'year', 2592000 => 'month', 604800 => 'week', 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second' ); foreach ($tokens as $unit => $text) { if ($difference < $unit) continue; $units = floor($difference / $unit); return $units . ' ' . $text . (($units > 1) ? 's' : ''); } } echo 'Event occurred ' . humanTiming($timestamp) . ' ago'; ?>
Explanation:
The above is the detailed content of How Can I Calculate and Display the Time Elapsed Since a DateTime Stamp in PHP?. For more information, please follow other related articles on the PHP Chinese website!