我有一些字符串,如" 3天前"," 6小时前"," 9分钟前"," 12秒前&# 34 ;.我是正则表达式的新手,所以我不确定如何匹配" s"之前"前"所以如果需要我可以把它剥掉。
修改:我的代码,以防它可以帮助其他人......
function timeAgo ($timestamp) {
$difference = time() - strtotime($timestamp);
if ($difference > (60*60*24)) {
$difference = round($difference/60/60/24) . " days ago";
}
else if ($difference > (60*60)) {
$difference = round($difference/60/60) . " hours ago";
}
else if ($difference > 60) {
$difference = round($difference/60) . " minutes ago";
}
else if ($difference > 0) {
$difference = $difference . " seconds ago";
}
$int = filter_var($difference, FILTER_SANITIZE_NUMBER_INT);
if ($int == 1) {
$difference = preg_replace('/.(?= ago)/', '', $difference);
}
return $difference;
}