mysql datetime 差异,从MYSQL datetime字段获取确切的时间差异

I want to do what StackOverflow is doing which is saying exactly how long it's been since the last post. There is a catch though, SO displays certain information based on how long the ago the last post was - for example, if the post was less than a day ago, they post how many hours ago the last post was; if the post was less than an hour ago they post how many minutes ago it was, etc.

I'm working with a MYSQL DateTime field in the following format:

2012-09-19 13:28:45

I want to compare the above to the time NOW and so I converted that time using PHP's strtotime function and tried comparing the two dates through a function I put together (below). Granted, this is probably the WORST possible way of doing this but after reading about PHP's Date and DateTime functions I'm starting to become very, very confused.

function get_date_format($strToTimeString){

$minute = 60;

$hour = $minutes * 60;

$day = $hour * 24;

$week = $day * 7;

$month = $week * 4;

$year = $month * 12;

$timeNow = strtotime("now");

$timeDiff = $timeNow - $strToTimeString;

if($timeDiff > $minute){

if($timeDiff > $hour){

if($timeDiff > $day){

if($timeDiff > $week){

if($timeDiff > $month){

if($timeDiff > $year){

// Years ago

}

else{

// Months ago

}

}

else{

// Weeks ago

}

}

else{

// Days ago

}

}

else

{

// Hours ago

}

}

else{

// Minutes ago

}

}

else{

// Seconds ago

}

}

Is there a better way to do this? As I mentioned above, I had no luck when trying to use DateTime->diff

I really appreciate any help.

解决方案

Use DateTime and DateTime:diff then check each value:

function returnInterval($date){

$datetime1 = new DateTime($date);

$datetime2 = new DateTime();

$diff = $datetime1->diff($datetime2);

$string = '';

$pass = '';

if($diff->y){

$string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";

$pass = ', ';

}

if($diff->m){

$string .= $pass;

$string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";

$pass = ', ';

}

if($diff->d){

$string .= $pass;

$string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";

$pass = ', ';

}

if($diff->h){

$string .= $pass;

$string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";

$pass = ', ';

}

if($diff->i){

$string .= $pass;

$string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";

$pass = ', ';

}

if($diff->s){

$string .= $pass;

$string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";

}

$pos = strrpos($string, ',');

$string = substr_replace($string, ' and ', $pos, 2);

return $string;

}

echo returnInterval('2012-09-19 13:28:45');

// 8 days, 13 hours, 47 minutes and 44 seconds

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值