1

So i have this function to show the time generated from the inserted unix format timestamp(time()):

function get_time($timestamp) {
    $time = date('G:i', $timestamp);
    echo " at: ".$time;
    return $time;
}

I would like to expand this function and would like to do so if it has been under 24 hours, it should calculate and echo "x hours ago", if its yesterday or later, it should just echo like it does now ( echo " at: ".$time; ) ..

How can this be done?

Karem
  • 17,615
  • 72
  • 178
  • 278
  • What is the difficulty you're encountering? – Artefacto Aug 12 '10 at 17:11
  • difficulty? Just a nice/simple way it can be done, not really a special difficulty "level" im seeking. (sorry if i misunderstood ur question, im not english) – Karem Aug 12 '10 at 17:14
  • possible duplicate of [PHP How to find the time elapsed since a date time?](http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time) – raveren Aug 19 '15 at 09:18

2 Answers2

6

Less than 24 hours ago is 3600*24, i.e.

$time_now-3600*24

then

if($timestamp > (time() - (3600*24))) {

// Calculate time difference

$diff = time() - $timestamp;

$hours = $diff / 3600;

echo $hours . " hours ago";

} else {

echo "at" . date("x-y-z", $timestamp");

}
matiasf
  • 1,098
  • 1
  • 9
  • 17
  • time is in seconds, the division should be `$diff / (24 * 3600)` – Bob Fincheimer Aug 12 '10 at 17:18
  • 1
    couple of edits: dbl quotes by timestamp and $diff/3600 not 24. Otherwise a solid way to approach it; create a function and add a few options (..."days ago", 'Last Wednesday' etc) and you are in good shape. – Hans Aug 12 '10 at 17:23
  • @Hans: Tanks, updated the code. $diff/3600 is of course correct. – matiasf Aug 12 '10 at 17:26
  • @matiasf i just tested this code, and output i am getting is 0.584444444444 hours ago. Everytime i refresh the page the" 0.584444444444 hours ago" changes to other different value like 0.621666666667 and so on..? – Karem Aug 12 '10 at 17:37
  • @Karem: You should cast the $diff to an integer with intval($diff). For recent timestamps a separate check could be performed and a minute value echoed instead. – matiasf Aug 12 '10 at 17:41
  • @Matiasf this is not what ive wanted, i mean i wanted "round" numbers, 1 hour, 2 hour, not 0.863333333333 hours ago, (if under 1 hour then show in minutes) or forget the under hour, how do i round it to 1 or 2 numbers. $hours = intval($diff) / 3600 dont do any difference – Karem Aug 12 '10 at 17:54
  • Why not just do "less than a hour ago" if it's < 1? That's what a lot of sites do. Or "About one day" etc. Just a long switch statement away from being whatever you need it to be. – Hans Aug 12 '10 at 20:53
0
/*
* @author Alexander.Plutov
* @param int $timestamp
* @param float $hours_ago
* @return date
*/
function get_time($timestamp, $hours_ago) {
    $seconds_ago = $hours_ago * 60 * 60 * 24;
    $new_timestamp = $timestamp - $seconds_ago;
    $time = date('G:i', $new_timestamp);
    echo " at: ".$time;
    return $time;
}
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143