在我正在开发的网站中,我希望在访问部分中实现stackoverflow的userprofile“see”,它说我们上次见到的时候。这给了我约会。但我想要“2h ago”。如何做到这一点? 我试过这个:
<?php
if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
}
setcookie(AboutVisit, time ()) ;
if (isset ($last))
{
$change = time () - $last;
if ( $change > 60)
{
echo "Welcome back! <br> You last visited on ". date("m/d/y",$last) ;
// Tells the user when they last visited
}
else
{
echo "Welcome to our site!";
//Greets a first time user
}
}
?>
答案 0 :(得分:0)
只需在登录cookie上使用时间戳即可。如果用户第一次登录,则cookie不会提前出现,因此您可以提供“欢迎来到我们的网站!”否则,只要用户登录,就会在登录cookie上标记日期和时间,然后使用该值显示“上次看到”的时间。
答案 1 :(得分:0)
以下功能将计算开始日期和结束日期之间的时间跨度。您可以根据自己的意愿修改它以格式化时间跨度:
/**
* Calculates the timespan between the start and end datetime
* @param timestamp $start The start timestamp for the time span
* @param timestamp $end The end timestamp for the time span
* @return string A string with the matched timespan
*/
public function timespan( $start, $end )
{
$seconds = $end - $start;
$days = floor( $seconds / 60 / 60 / 24 );
$hours = $seconds / 60 / 60 % 24;
$mins = $seconds / 60 % 60;
$secs = $seconds % 60;
$duration = "";
if ( $days > 0 )
{
$hr = ($hours > 1) ? " $hours hours" : (($hours > 0) ? " $hours hour" : "");
$duration .= ($days > 1) ? "$days days$hr ago" : "$days day$hr ago";
}
elseif ( $hours > 0 )
{
$mi = ($mins > 1) ? " $mins minutes" : (($mins > 0) ? " $mins minute" : "");
$duration .= ($hours > 1) ? "$hours hours$mi ago" : "$hours hour$mi ago";
}
elseif ( $mins > 0 )
{
$duration .= ($mins > 1) ? "$mins minutes ago" : "$mins minute ago";
}
elseif ( $secs > 0 )
{
$duration .= ($secs > 1) ? "$secs seconds ago" : "$secs second ago";
}
$duration = trim( $duration );
if ( $duration == null )
$duration = '0 seconds ago';
return $duration;
}
祝你好运!