Jump to content

Recommended Posts

okay what i have is a database table with a timestamp in it..

 

so when i echo this

 

echo "<td>" . $spy['time'] . "</td>\n";

 

it displays the time and date....

 

 

well what i wanna do is display the time since the timestamp.

 

so if it was 10 minutes ago it will say 10m or whatever..

 

so i tried using this...

 

echo "<td>" . timeSince($spy['time']) . "</td>\n";

 

but its echoing this: 43y 217d 16h 31m 37s

 

thats obviously wrong because i wasnt even alive then lol.

 

 

does anyone know why this is happening? and how to fix it?

 

 

Link to comment
https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/
Share on other sites

i didnt write this... but here it is.

 

function timeSince($urtime)
   {
      $seconds   = (((time() - $urtime) / 1) % 60);
      $minutes   = (intval((time() - $urtime) / 60) % 60);
      $hours      = intval((time() - $urtime) / 3600) % 24;
      $days      = intval((time() - $urtime) / 86400) % 365;
      $years      = intval((time() - $urtime) / 29030400);
      $result      = array();
      $result['years']   = ($years) ? sprintf('%uy%s', number_format($years), ($years > 1) ? '' : '') : FALSE;
      $result['days']   = ($days) ? sprintf('%ud%s', number_format($days), ($days > 1) ? '' : '') : FALSE;
      $result['hours']   = ($hours) ? sprintf('%uh%s', number_format($hours), ($hours > 1) ? '' : '') : FALSE;
      $result['minutes']   = ($minutes) ? sprintf('%um%s', $minutes, ($minutes > 1) ? '' : FALSE) : FALSE;
      $result['seconds']   = ($seconds) ? sprintf('%s%us%s',($minutes) ? '' : '', $seconds, ($seconds > 1) ? '' : FALSE) : FALSE;
      if ($seconds < 1) return "NOW!";
      else return (count($result) == 0) ? 'Never' : implode(' ', $result).'';
   } 

I made this class ages ago but I still use it as it works perfectly:

 

<?php

   class TimeSince{
   
      static function since($time){
         
         $now = mktime();
         $seconds_since = $now - $time;
         
         if($seconds_since < 60){
            return $seconds_since.' seconds ago';
         }
         else if($seconds_since > 59 && $seconds_since < 120){
            return '1 minute ago';
         }
         else if($seconds_since > 119 && $seconds_since < 3600){
            $minutes = round($seconds_since / 60);
            return $minutes.' minutes ago';
         }
         else if($seconds_since > 3599 && $seconds_since < 86400){
            $hours = round($seconds_since / 3600);
            if($hours == 1){
               return "1 hour ago";
            }
            else{
               return $hours.' hours ago';
            }
         }
         else if($seconds_since > 86400 && $seconds_since < 604800){
            $days = round($seconds_since/86400);
            if($days == 1){
               return "1 day ago";
            }
            else{
               return $days.' days ago';
            }
         }
         else if($seconds_since > 604799){
            $weeks = round($seconds_since/604800);
            if($weeks == 1){
               return "1 week ago";
            }
            else{
               return $weeks.' weeks ago';
            }
         }
      }
   }
?>

 

To use it just do:

 

require_once("time.class.php"); //or whatever filename you give the above code
$timesince = strtotime($spy['time']);
echo TimeSince::since($timesince);

You said:

 

so when i echo this

 

echo "<td>" . $spy['time'] . "</td>\n";

 

it displays the time and date....

 

That would indicate a formatted string, but your function seems to take a UNIX timestamp as argument. That could possibly explain the unexpected behavior.

I made this class ages ago but I still use it as it works perfectly:

 

<?php

   class TimeSince{
   
      static function since($time){
         
         $now = mktime();
         $seconds_since = $now - $time;
         
         if($seconds_since < 60){
            return $seconds_since.' seconds ago';
         }
         else if($seconds_since > 59 && $seconds_since < 120){
            return '1 minute ago';
         }
         else if($seconds_since > 119 && $seconds_since < 3600){
            $minutes = round($seconds_since / 60);
            return $minutes.' minutes ago';
         }
         else if($seconds_since > 3599 && $seconds_since < 86400){
            $hours = round($seconds_since / 3600);
            if($hours == 1){
               return "1 hour ago";
            }
            else{
               return $hours.' hours ago';
            }
         }
         else if($seconds_since > 86400 && $seconds_since < 604800){
            $days = round($seconds_since/86400);
            if($days == 1){
               return "1 day ago";
            }
            else{
               return $days.' days ago';
            }
         }
         else if($seconds_since > 604799){
            $weeks = round($seconds_since/604800);
            if($weeks == 1){
               return "1 week ago";
            }
            else{
               return $weeks.' weeks ago';
            }
         }
      }
   }
?>

 

To use it just do:

 

require_once("time.class.php"); //or whatever filename you give the above code
$timesince = strtotime($spy['time']);
echo TimeSince::since($timesince);

 

i just tried that but now its echos "14 hours ago"

I made this class ages ago but I still use it as it works perfectly:

 

<?php

   class TimeSince{
   
      static function since($time){
         
         $now = mktime();
         $seconds_since = $now - $time;
         
         if($seconds_since < 60){
            return $seconds_since.' seconds ago';
         }
         else if($seconds_since > 59 && $seconds_since < 120){
            return '1 minute ago';
         }
         else if($seconds_since > 119 && $seconds_since < 3600){
            $minutes = round($seconds_since / 60);
            return $minutes.' minutes ago';
         }
         else if($seconds_since > 3599 && $seconds_since < 86400){
            $hours = round($seconds_since / 3600);
            if($hours == 1){
               return "1 hour ago";
            }
            else{
               return $hours.' hours ago';
            }
         }
         else if($seconds_since > 86400 && $seconds_since < 604800){
            $days = round($seconds_since/86400);
            if($days == 1){
               return "1 day ago";
            }
            else{
               return $days.' days ago';
            }
         }
         else if($seconds_since > 604799){
            $weeks = round($seconds_since/604800);
            if($weeks == 1){
               return "1 week ago";
            }
            else{
               return $weeks.' weeks ago';
            }
         }
      }
   }
?>

 

To use it just do:

 

require_once("time.class.php"); //or whatever filename you give the above code
$timesince = strtotime($spy['time']);
echo TimeSince::since($timesince);

 

i just tried that but now its echos "14 hours ago"

 

Yes? It starts with seconds, then minutes, then hours, then days and finally weeks. Seeing "19028 minutes ago" is pretty messy.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.