We have a simple function which return the duration between the time() and the unix time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php $createdAt = 1601434800; echo '$createdAt = ' . $createdAt . "\r\n"; $date = new DateTime('@' . $createdAt); $interval = $date->diff(new DateTime('@' . time())); // var_dump($interval); // echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ".$interval->h." hours ".$interval->i." minutes ".$interval->s." seconds"."ago"; function getDuration ($interval) { // echo "\r\n".$interval->i."\r\n"; if ($interval->d) { if ($interval->h) { return 'published '.$interval->d.' days '.$interval->h.' hours ago'; } else { return 'published '.$interval->d.' days ago'; } } else if ($interval->h) { if ($interval->i) { return 'published '.$interval->h.' hours '.$interval->i.' minutes ago'; } else { return 'published '.$interval->h.' hours ago'; } } else if ($interval->i) { if ($interval->i) { return 'published '.$interval->i.' minutes '.$interval->s.' seconds ago'; } else { return 'published '.$interval->s.' seconds ago'; } } } $duration = getDuration($interval); echo '$duration = '.$duration."\r\n"; ?> |
That’s it 🙂