PDA

View Full Version : PHP Question



Luke
10-01-2010, 10:33 AM
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?

Blinger1
10-01-2010, 10:41 AM
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

Luke
10-01-2010, 10:46 AM
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?

Invent
10-01-2010, 12:36 PM
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

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 * 7 ), '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 ][ 0 ];
$name = $chunks[ $i ][ 1 ];

if( ( $count = floor( $since / $seconds ) ) != 0 ) {

break;

}

}

$print = ( $count == 1 ) ? '1 ' . $name : $count . ' ' . $name . 's';

if( ( $i + 1 ) < $j ) {

$seconds2 = $chunks[ ( $i + 1 ) ][ 0 ];
$name2 = $chunks[ ( $i + 1 ) ][ 1 ];

if( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {

$print .= ( $count2 == 1 ) ? ', 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 :P

You can use it like this:



<?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..

Luke
10-01-2010, 12:42 PM
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

Invent
10-01-2010, 12:45 PM
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

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 * 7 ), '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 ][ 0 ];
$name = $chunks[ $i ][ 1 ];

if( ( $count = floor( $since / $seconds ) ) != 0 ) {

break;

}

}

$print = ( $count == 1 ) ? '1 ' . $name : $count . ' ' . $name . 's';

if( ( $i + 1 ) < $j ) {

$seconds2 = $chunks[ ( $i + 1 ) ][ 0 ];
$name2 = $chunks[ ( $i + 1 ) ][ 1 ];

if( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {

$print .= ( $count2 == 1 ) ? ', 1 ' . $name2 : ' ' . $count2 . ' ' . $name2 . 's';

}

}

$print .= ' ago';

return $print;

}

}




$time = TimeHandler::getInstance()->setTime( time() )->getFriendly();

// or

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

echo $time . ' - ' . $time2;

Luke
10-01-2010, 01:03 PM
Oops, use this:



<?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 * 7 ), '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 ][ 0 ];
$name = $chunks[ $i ][ 1 ];

if( ( $count = floor( $since / $seconds ) ) != 0 ) {

break;

}

}

$print = ( $count == 1 ) ? '1 ' . $name : $count . ' ' . $name . 's';

if( ( $i + 1 ) < $j ) {

$seconds2 = $chunks[ ( $i + 1 ) ][ 0 ];
$name2 = $chunks[ ( $i + 1 ) ][ 1 ];

if( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {

$print .= ( $count2 == 1 ) ? ', 1 ' . $name2 : ' ' . $count2 . ' ' . $name2 . 's';

}

}

$print .= ' ago';

return $print;

}

}




$time = TimeHandler::getInstance()->setTime( time() )->getFriendly();

// or

$instance = new TimeHandler();
$time2 = $instance->setTime( time() )->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

Want to hide these adverts? Register an account for free!