基于微信朋友圈实现时间显示样式
计算时间差
$startdate=date('Y-m-d H:i:s');
$enddate="2020-12-12 12:12:12";
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
echo "相差天数:".$date."天<br><br>";
$hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
echo "相差小时数:".$hour."小时<br><br>";
$minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
echo"相差分钟数:".$minute."分钟<br><br>";
$second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
echo"相差秒数:".$second."秒";
基于微信朋友圈实现时间显示样式
public function timer(array $array)
{
$today = date('Y-m-d H:i:s');
$D = date('d');
$H = date('H');
$M = date('i');
foreach ($array as $key => $value) {
$d = substr($value['create_time'], 8, 2);
$h = substr($value['create_time'], 11, 2);
$m = substr($value['create_time'], 14, 2);
$date = (strtotime($today) - strtotime($value['create_time'])) / 86400;
switch ($date) {
case $date < 1:
if ($D != $d) {
$hour = 24 - $h + $H;
} else {
$hour = abs($H - $h);
}
if ($hour < 1) {
if ($m == $M) {
$array[$key]['create_time'] = "刚刚";
break;
} else if ($m < $M) {
$minute = $M - $m;
$array[$key]['create_time'] = $minute . "分钟前";
break;
} else {
$minute = 60 - $m + $M;
$array[$key]['create_time'] = $minute . "分钟前";
break;
}
} else {
$array[$key]['create_time'] = $hour . "小时前";
break;
}
break;
case $date < 2:
$array[$key]['create_time'] = "昨天";
break;
case $date < 3:
$array[$key]['create_time'] = "两天前";
break;
default:
$array[$key]['create_time'] = $array[$key]['create_time'];
break;
}
}
return $array;
}