Home > Article > Backend Development > How to return relative time in php (eg: 20 minutes ago, 3 days ago)_PHP Tutorial
This article describes the example of php returning relative time (e.g.: 20 minutes ago, 3 days ago) ) method. Share it with everyone for your reference. The details are as follows:
4 11 12 |
function plural($num) { if ($num != 1) return "s"; } function getRelativeTime($date) { $diff = time() - strtotime($date); if ($diff<60) return $diff." seconds".plural($diff)." before"; $diff = round($diff/60); if ($diff<60) return $diff." minutes".plural($diff)." ago"; $diff = round($diff/60); if ($diff<24) return $diff." hours".plural($diff)." ago"; $diff = round($diff/24); if ($diff<7) return $diff." day".plural($diff)." before"; $diff = round($diff/7); if ($diff<4) return $diff." week".plural($diff)." ago"; return "on ".date("F j, Y", strtotime($date)); } |