Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    75
    Thanks
    1
    Thanked 2 Times in 2 Posts

    PHP timestamp to real time

    Hey guys. I'm using this function and I want to know if I could do something like what Facebook does. I know this function has the X minutes ago, but I was wondering if there's a way to make it so that when it pasts 24 hours, it shows the timestamp.

    PHP Code:
    if(!function_exists('time_stamp')){
    function 
    time_stamp($session_time) {

        
    $time_difference time() - $session_time ;

        
    $seconds    $time_difference ;
        
    // Seconds
        
    if($seconds <= 60) {
            return 
    "$seconds seconds ago";
        }

        
    $minutes    round($time_difference 60 );
        
    //Minutes
        
    if($minutes <= 60) {
            if(
    $minutes == 1) {
                return 
    "one minute ago";
            } else {
                return 
    "$minutes minutes ago";
            }
        }
       
        
    $hours      round($time_difference 3600 );
        
    //Hours
        
    if($hours <= 24) {
            if(
    $hours == 1) {
                return 
    "one hour ago";
            } else {
                return 
    "$hours hours ago";
            }
        }
       
        
    $days      round($time_difference 86400 );
        
    //Days
        
    if($days <= 7) {
            if(
    $days == 1) {
                return 
    "one day ago";
            } else {
                return 
    "$days days ago";
            }
        }
       
        
    $weeks      round($time_difference 604800 );
        
    //Weeks
        
    if($weeks <= 4) {
            if(
    $weeks == 1) {
                return 
    "one week ago";
            }else{
                return 
    "$weeks weeks ago";
            }
        }
       
        
    $months    round($time_difference 2419200 );
        
    //Months
        
    if($months <= 12) {
            if(
    $months==1) {
                return 
    "one month ago";
            } else {
                return 
    "$months months ago";
            }
        }
       
        
    $years      round($time_difference 29030400 );
        
    //Years
        
    if($years==1) {
            return 
    "one year ago";
        } else {
            return 
    "$years years ago";
        }

    }
    }

    $timed $timestamp
    PHP Timestamp echo:
    PHP Code:
    <?
    $timestamp 
    $row['timestamped'];

    // $timestamp_modified = date('l, F jS, Y g:i A', $timestamp);
    // ^ that is the timestamp I want to show when 24 hours has past.
    ?>
    <? 
    echo time_stamp("$timed"); ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What version of PHP are you using?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    75
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    What version of PHP are you using?
    I'm using PHP 5.3

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Make use of the DateInterval:
    PHP Code:
    function pastPlural($iNum$sDescription)
    {
        return 
    sprintf('%s %s ago',
          (
    $iNum == 'one' $iNum),
          (
    $iNum $sDescription 's' $sDescription));
    }

    function 
    time_stamp($session_time)
    {
       
    $sResult '';
       if (
    is_int($session_time))
       {
          
    $dtSession = new Datetime();
          
    $dtSession->setTimestamp($session_time);
       }
       else if (
    is_string($session_time))
       {
          
    $dtSession = new DateTime($session_time);
       }
       else if (
    $session_time instanceof DateTime)
       {
          
    $dtSession $session_time;
       }

       if (!(
    $dtSession instanceof DateTime))
       {
          
    trigger_error('Session time provided is invalid');
       }
       else
       {
          
    $aOffsets = array(
             
    'y' => 'year',
             
    'm' => 'month',
             
    'd' => 'day',
             
    'h' => 'hour',
             
    'i' => 'minute',
             
    's' => 'second'
          
    );
          
    $dtNow = new DateTime();
          
    $dtDiff $dtNow->diff($dtSession);
          while (empty(
    $sResult) && $desc current($aOffsets))
          {
             
    $id key($aOffsets);
             
    next($aOffsets);
             if (
    < ($val $dtDiff->$id))
             {
                
    $sResult pastPlural($val$desc);
             }
          }
       }
       return 
    $sResult;

    If you can ensure that the session time is always in a DateTime format, you could typehint the method and remove all of the is_* checks.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •