PHP: An hour ago, x hours ago
So i have this function to show the time generated from the inserted unix format 开发者_运维技巧timestamp(time()):
function get_time($timestamp) {
$time = date('G:i', $timestamp);
echo " at: ".$time;
return $time;
}
I would like to expand this function and would like to do so if it has been under 24 hours, it should calculate and echo "x hours ago", if its yesterday or later, it should just echo like it does now ( echo " at: ".$time; ) ..
How can this be done?
Less than 24 hours ago is 3600*24, i.e.
$time_now-3600*24
then
if($timestamp > (time() - (3600*24))) {
// Calculate time difference
$diff = time() - $timestamp;
$hours = $diff / 3600;
echo $hours . " hours ago";
} else {
echo "at" . date("x-y-z", $timestamp");
}
/*
* @author Alexander.Plutov
* @param int $timestamp
* @param float $hours_ago
* @return date
*/
function get_time($timestamp, $hours_ago) {
$seconds_ago = $hours_ago * 60 * 60 * 24;
$new_timestamp = $timestamp - $seconds_ago;
$time = date('G:i', $new_timestamp);
echo " at: ".$time;
return $time;
}
精彩评论