Results 1 to 7 of 7

Thread: PHP Question

  1. #1
    Join Date
    Nov 2006
    Location
    D?sseldorf
    Posts
    2,858
    Tokens
    2,256

    Latest Awards:

    Default PHP Question

    Hi

    I'm experimenting with php, and grabbing my twitter xml and echoing it as text.
    Now, I have a variable $created_at which shows when the tweet was sent.

    At the moment, that echo's "Sun Jan 10 10:19:05 +0000 2010"
    Has anyone any idea how I could turn that into an output, that would show "23 seconds ago" or something like that?

    Many Thanks
    Luke

    EDIT: Or has anyone a better way of display the tweets?
    Last edited by Luke; 10-01-2010 at 10:40 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:

    Default

    Google "difference between two dates" a lot of links there

    http://www.go4expert.com/forums/showthread.php?t=2769

    might be what u are after

  3. #3
    Join Date
    Nov 2006
    Location
    D?sseldorf
    Posts
    2,858
    Tokens
    2,256

    Latest Awards:

    Default

    Quote Originally Posted by Blinger View Post
    Google "difference between two dates" a lot of links there

    http://www.go4expert.com/forums/showthread.php?t=2769

    might be what u are after
    Hmm, that helps, a little, I know how tot do it, just not sure how to do it, with "current time" - "twitter status time". Not a legend @ php lol

    Would +rep but gotta spread ;[

    Any other ideas?

  4. #4
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Quote Originally Posted by LukeBateson View Post
    Hmm, that helps, a little, I know how tot do it, just not sure how to do it, with "current time" - "twitter status time". Not a legend @ php lol

    Would +rep but gotta spread ;[

    Any other ideas?
    Try something like this:

    PHP Code:
    <?php

    class TimeHandler {
        
        protected 
    $_unix;
        
        protected static 
    $_instance;

        public function 
    __construct() {
        
            
    parent::__construct();
            
        }

        public static function 
    getInstance() {

            if( ( 
    self::$_instance instanceof self ) !== true ) {

                
    self::$_instance = new self();

            }

            return 
    self::$_instance;

        }
            
        public function 
    setTime$time ) {
            
            
    $this->_unix $time;
            
            return 
    $this;
        
        }
        
        public function 
    getFriendly() {

            
    $chunks = array(

                array( ( 
    60 60 24 365 ), 'year' ),
                array( ( 
    60 60 24 30 ), 'month' ),
                array( ( 
    60 60 24 ), 'week' ),
                array( ( 
    60 60 24 ), 'day' ),
                array( ( 
    60 60 ), 'hour' ),
                array( 
    60'minute' ),
                array( 
    1'second' )
            
            );
        
            
    $today    time();
            
    $since    = ( $today $this->_unix );
            
    $size    count$chunks );
            
            for( 
    $i 0$j $size$i $j$i++ ) {
            
                
    $seconds    $chunks$i ][ ];
                
    $name        $chunks$i ][ ];
            
                if( ( 
    $count floor$since $seconds ) ) != ) {
        
                    break;
                
                }
                
            }
        
            
    $print = ( $count == ) ? '1 ' $name $count ' ' $name 's';
        
            if( ( 
    $i ) < $j ) {
            
                
    $seconds2    $chunks[ ( $i ) ][ ];
                
    $name2        $chunks[ ( $i ) ][ ];
        
                if( ( 
    $count2 floor( ( $since - ( $seconds $count ) ) / $seconds2 ) ) != ) {
                
                    
    $print .= ( $count2 == ) ? ', 1 ' $name2 ' ' $count2 ' ' $name2 's';
                
                }
                
            }
            
            
    $print .= ' ago';
           
            return 
    $print;
        
        }
        
    }
    Sorry about the fact it's in a class but I don't have time to take it out of it

    You can use it like this:

    PHP Code:
    <?php

        $time 
    TimeHander::getInstance()->setTime'unixTimestampGoesHere' )->getFriendly();

        
    // or

        
    $instance   = new TimeHandler();
        
    $time       $instance->setTime'unixTimestampGoesHere' )->getFriendly();

    ?>
    The getFriendly method itself isn't the best but it should do what you want..
    Last edited by Invent; 10-01-2010 at 12:38 PM.

  5. #5
    Join Date
    Nov 2006
    Location
    D?sseldorf
    Posts
    2,858
    Tokens
    2,256

    Latest Awards:

    Default

    Didn't work ;[

    Hmm, twitter has put it in such an anoying format, think I'll have to find another way to show it haha

    Thanks anyways; +rep (again: gotta spread :/ lol)
    Luke

  6. #6
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Quote Originally Posted by LukeBateson View Post
    Didn't work ;[

    Hmm, twitter has put it in such an anoying format, think I'll have to find another way to show it haha

    Thanks anyways; +rep
    Luke
    Oops, use this:

    PHP Code:
    <?php

    class TimeHandler {
        
        protected 
    $_unix;
        
        protected static 
    $_instance;

        public function 
    __construct() {        
        }

        public static function 
    getInstance() {

            if( ( 
    self::$_instance instanceof self ) !== true ) {

                
    self::$_instance = new self();

            }

            return 
    self::$_instance;

        }
            
        public function 
    setTime$time ) {
            
            
    $this->_unix $time;
            
            return 
    $this;
        
        }
        
        public function 
    getFriendly() {

            
    $chunks = array(

                array( ( 
    60 60 24 365 ), 'year' ),
                array( ( 
    60 60 24 30 ), 'month' ),
                array( ( 
    60 60 24 ), 'week' ),
                array( ( 
    60 60 24 ), 'day' ),
                array( ( 
    60 60 ), 'hour' ),
                array( 
    60'minute' ),
                array( 
    1'second' )
            
            );
        
            
    $today    time();
            
    $since    = ( $today $this->_unix );
            
    $size    count$chunks );
            
            for( 
    $i 0$j $size$i $j$i++ ) {
            
                
    $seconds    $chunks$i ][ ];
                
    $name        $chunks$i ][ ];
            
                if( ( 
    $count floor$since $seconds ) ) != ) {
        
                    break;
                
                }
                
            }
        
            
    $print = ( $count == ) ? '1 ' $name $count ' ' $name 's';
        
            if( ( 
    $i ) < $j ) {
            
                
    $seconds2    $chunks[ ( $i ) ][ ];
                
    $name2        $chunks[ ( $i ) ][ ];
        
                if( ( 
    $count2 floor( ( $since - ( $seconds $count ) ) / $seconds2 ) ) != ) {
                
                    
    $print .= ( $count2 == ) ? ', 1 ' $name2 ' ' $count2 ' ' $name2 's';
                
                }
                
            }
            
            
    $print .= ' ago';
           
            return 
    $print;
        
        }
        
    }
    PHP Code:
    $time TimeHandler::getInstance()->setTimetime() )->getFriendly();

    // or

    $instance   = new TimeHandler();
    $time2       $instance->setTimetime() )->getFriendly();

    echo 
    $time ' - ' $time2

  7. #7
    Join Date
    Nov 2006
    Location
    D?sseldorf
    Posts
    2,858
    Tokens
    2,256

    Latest Awards:

    Default

    Quote Originally Posted by Invent View Post
    Oops, use this:

    PHP Code:
    <?php

    class TimeHandler {
        
        protected 
    $_unix;
        
        protected static 
    $_instance;

        public function 
    __construct() {        
        }

        public static function 
    getInstance() {

            if( ( 
    self::$_instance instanceof self ) !== true ) {

                
    self::$_instance = new self();

            }

            return 
    self::$_instance;

        }
            
        public function 
    setTime$time ) {
            
            
    $this->_unix $time;
            
            return 
    $this;
        
        }
        
        public function 
    getFriendly() {

            
    $chunks = array(

                array( ( 
    60 60 24 365 ), 'year' ),
                array( ( 
    60 60 24 30 ), 'month' ),
                array( ( 
    60 60 24 ), 'week' ),
                array( ( 
    60 60 24 ), 'day' ),
                array( ( 
    60 60 ), 'hour' ),
                array( 
    60'minute' ),
                array( 
    1'second' )
            
            );
        
            
    $today    time();
            
    $since    = ( $today $this->_unix );
            
    $size    count$chunks );
            
            for( 
    $i 0$j $size$i $j$i++ ) {
            
                
    $seconds    $chunks$i ][ ];
                
    $name        $chunks$i ][ ];
            
                if( ( 
    $count floor$since $seconds ) ) != ) {
        
                    break;
                
                }
                
            }
        
            
    $print = ( $count == ) ? '1 ' $name $count ' ' $name 's';
        
            if( ( 
    $i ) < $j ) {
            
                
    $seconds2    $chunks[ ( $i ) ][ ];
                
    $name2        $chunks[ ( $i ) ][ ];
        
                if( ( 
    $count2 floor( ( $since - ( $seconds $count ) ) / $seconds2 ) ) != ) {
                
                    
    $print .= ( $count2 == ) ? ', 1 ' $name2 ' ' $count2 ' ' $name2 's';
                
                }
                
            }
            
            
    $print .= ' ago';
           
            return 
    $print;
        
        }
        
    }
    PHP Code:
    $time TimeHandler::getInstance()->setTimetime() )->getFriendly();

    // or

    $instance   = new TimeHandler();
    $time2       $instance->setTimetime() )->getFriendly();

    echo 
    $time ' - ' $time2
    Looks interesting lol, on iphone atm, so I'll have a play round with it when I'm back at workstation - cheers.

    Luke

Posting Permissions

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