Twitsnip is a place to post code snippets and share them. Login with your existing Twitter account, if you don't have a Twitter account simply create on at www.twitter.com

Twitter style time stamp

Twitter style time stamp

0

This function takes the date and time and returns how much time has passed, just like twitter does. for example 10 seconds ago

function post_time($time){

	$format = "F m, Y g:i a";

	$timeyear = 365 * 24 * 60 * 60;
	$timemonth = 30 * 24 * 60 * 60;
	$timeweek = 7 * 24 * 60 * 60;
	$timeday = 24 * 60 * 60;
	$timehour = 60 * 60;
	$timemins = 60;
	$timeseconds = 1;

	$today = time();

	$x = $today - strtotime($time);

	if($x >= $timeyear){$x = date($format, $x); $dformat=""; $pre ="On the date: "; 
	}elseif($x >= $timemonth){$x = date($format, $x); $dformat=""; $pre ="On the date: ";
	}elseif($x >= $timeday){$x = round($x / $timeday); $dformat="days ago"; $pre =" "; $x = round($x);
	}elseif($x >= $timehour){$x = round($x / $timehour); $dformat="hours ago"; $pre =" "; 
	}elseif($x >= $timemins){$x = round($x / $timemins); $dformat="minutes ago"; $pre =" ";
	}elseif($x >= $timeseconds){$x = round($x / $timeseconds); $dformat="seconds ago"; $pre =" "; 
	}
	return $pre." ".$x." ".$dformat;
}

Tags: php, twitter, time, stamp, post, date

Comments

No comments yet

Login to Twitter to comment