Forum Moderators: coopster
<?php
function difference_in_time($input) {
if(preg_match('/^[a-z]+\s([a-z]+)\s([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s\+[0-9]{4}\s([0-9]{4})$/is', $input, $out)) {
if(strtolower($out[1]) === "jan") {
$month = 1;
} elseif(strtolower($out[1]) === "feb") {
$month = 2;
} elseif(strtolower($out[1]) === "mar") {
$month = 3;
} elseif(strtolower($out[1]) === "apr") {
$month = 4;
} elseif(strtolower($out[1]) === "may") {
$month = 5;
} elseif(strtolower($out[1]) === "jun") {
$month = 6;
} elseif(strtolower($out[1]) === "jul") {
$month = 7;
} elseif(strtolower($out[1]) === "aug") {
$month = 8;
} elseif(strtolower($out[1]) === "sep") {
$month = 9;
} elseif(strtolower($out[1]) === "oct") {
$month = 10;
} elseif(strtolower($out[1]) === "nov") {
$month = 11;
} elseif(strtolower($out[1]) === "dec") {
$month = 12;
} else {
$month_check = 1;
}
if(!isset($month_check) || $month_check !== 1) {
$old_time = mktime($out[3], $out[4], $out[5], $month, ltrim($out[2], "0"), $out[6]);
$output = (time() - $old_time);
} else {
$output = 'Invalid month format';
}
} else {
$output = 'Invalid input';
}
return $output;
}
$some_date = 'Wed Apr 07 13:01:45 +0000 2010';
echo difference_in_time($some_date);
?>
<?
$username='flapane';
$format='json'; // format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable
function difference_in_time($input) {
if(preg_match('/^[a-z]+\s([a-z]+)\s([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s\+[0-9]{4}\s([0-9]{4})$/is', $input, $out)) {
if(strtolower($out[1]) === "jan") {
$month = 1;
} elseif(strtolower($out[1]) === "feb") {
$month = 2;
} elseif(strtolower($out[1]) === "mar") {
$month = 3;
} elseif(strtolower($out[1]) === "apr") {
$month = 4;
} elseif(strtolower($out[1]) === "may") {
$month = 5;
} elseif(strtolower($out[1]) === "jun") {
$month = 6;
} elseif(strtolower($out[1]) === "jul") {
$month = 7;
} elseif(strtolower($out[1]) === "aug") {
$month = 8;
} elseif(strtolower($out[1]) === "sep") {
$month = 9;
} elseif(strtolower($out[1]) === "oct") {
$month = 10;
} elseif(strtolower($out[1]) === "nov") {
$month = 11;
} elseif(strtolower($out[1]) === "dec") {
$month = 12;
} else {
$month_check = 1;
}
if(!isset($month_check) || $month_check !== 1) {
$old_time = gmmktime($out[3], $out[4], $out[5], $month, ltrim($out[2], "0"), $out[6]);
$output = $old_time;
} else {
$output = 'Invalid month format';
}
} else {
$output = 'Invalid input';
}
return $output;
}
function human_time_diff( $from, $to = '' ) {
//function stolen from wordpress
if ( empty($to) )
$to = time();
$diff = (int) abs($to - $from);
if ($diff <= 3600) {
$mins = round($diff / 60);
if ($mins <= 1)
$since = '1 min';
else
$since = sprintf('%s mins', $mins);
} else if (($diff <= 86400) && ($diff > 3600)) {
$hours = round($diff / 3600);
if ($hours <= 1)
$since = '1 hour';
else
$since = sprintf('%s hours', $hours );
} elseif ($diff >= 86400) {
$days = round($diff / 86400);
if ($days <= 1)
$since = '1 day';
else
$since = sprintf('%s days', $days );
}
return $since;
}
for ($i = 0; $i <= 4; $i++) {
echo $tweet[$i]->text;
echo "<br />";
echo $tweet[$i]->created_at;
echo "<br />";
$tempo = $tweet[0]->created_at;
$tempo2 = explode(" ", $tempo, 6);
$some_date = "$tempo2[0] $tempo2[1] $tempo2[2] $tempo2[3] $tempo2[4] $tempo2[5]";
echo "Data for" .($i+1). "th tweet<br />";
echo difference_in_time($some_date) ."<br />";
echo time() ."<br />";
$tempoforum = difference_in_time($some_date) ."<br />";
echo human_time_diff( time(), $tempoforum ) ." ago <br /><br />";
}
?>