php 数据库时间具体到分钟,从mysql数据库获取时间戳并获取php以天,小时,分钟计算时间? [重复]...

可能重复:PHP:格式化时间Stackoverflow或Apple Mail样式

嗨,有人可以帮助我,我回应了mysql表中的时间戳,但我希望php计算时间戳,以天/小时/分钟 .

这是可能的,我仍然是php和mysql的新手,我正在学习,所以有人可以指导我正确的方向或告诉我如何做到这一点 . 谢谢

echo "{$news['date_added']}";?>

我希望输出为秒数:添加12秒前或添加3分钟前或添加4天前或添加4个月前

我认为脚本应该是这样的:

function nicetime($date)

{

if(empty($date)) {

return "No date provided";

}

$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");

$lengths = array("60","60","24","7","4.35","12","10");

$now = time();

$unix_date = strtotime($date);

// check validity of date

if(empty($unix_date)) {

return "Bad date";

}

// is it future date or past date

if($now > $unix_date) {

$difference = $now - $unix_date;

$tense = "ago";

} else {

$difference = $unix_date - $now;

$tense = "from now";

}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {

$difference /= $lengths[$j];

}

$difference = round($difference);

if($difference != 1) {

$periods[$j].= "s";

}

return "$difference $periods[$j] {$tense}";

}

$date = "{$news['content']}";

$result = nicetime($date); // 2 days ago ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值