Php 如何实施";见;在我的网站上
在我正在开发的网站中,我想在访问部分实现stackoverflow的用户配置文件“seen”,上面写着我们最后一次被看到的时间。这是给我的日期。但我想知道“2h前”。怎么做? 我试过这个:Php 如何实施";见;在我的网站上,php,session-cookies,Php,Session Cookies,在我正在开发的网站中,我想在访问部分实现stackoverflow的用户配置文件“seen”,上面写着我们最后一次被看到的时间。这是给我的日期。但我想知道“2h前”。怎么做? 我试过这个: <?php if(isset($_COOKIE['AboutVisit'])) { $last = $_COOKIE['AboutVisit']; } setcookie(AboutVisit, time ()) ; if (iss
<?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
}
}
?>
只需使用登录cookie上的时间戳即可。如果用户是第一次登录,cookie将不会提前出现,因此您可以表示“欢迎访问我们的网站!”否则,每当用户登录时,在登录cookie上标记日期和时间,然后使用该值显示“上次看到”的时间。下面的函数将计算开始日期和结束日期之间的时间跨度。您可以根据自己的意愿修改时间跨度的格式:
/**
* 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;
}
祝你好运 只需存储上次访问的时间戳,然后从当前时间戳中减去它。并使用
“最后一次看到的”。日期('H',$timestamp)。“小时前”