从MYSQL datetime字段获取确切的时间差异 [英] Getting exact time difference from MYSQL datetime field

查看:168
本文介绍了从MYSQL datetime字段获取确切的时间差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做StackOverflow做什么,这是说自从上一篇文章已经有多长时间了。有一个catch,虽然,SO显示某些信息,基于多久前的最后一个帖子 - 例如,如果该帖子不到一天前,他们发布多少小时前最后一个帖子;



我使用的是MYSQL DateTime字段,格式如下:

/ p>


2012-09-19 13:28:45


我想比较上面的时间 NOW ,所以我使用PHP的 strtotime 函数并尝试通过一个函数比较两个日期放在一起(下)。当然,这可能是最好的方法,但在阅读关于PHP的 Date DateTime 开始变得非常非常混乱。

  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 {
//月前
}
}
else {
//周前
}
}
else {
//天前
}
}
else
{
//小时前
}
}
else {
//分钟前
}

}
else {
//秒前
}


}

有更好的方法吗?正如我上面提到的,当尝试使用 DateTime-> diff



时,我没有运气帮助。

解决方案

使用 DateTime DateTime :diff 然后检查每个值:

  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,'和',$ pos,2);
return $ string;
}

echo returnInterval('2012-09-19 13:28:45');
// 8天,13小时,47分44秒


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

这篇关于从MYSQL datetime字段获取确切的时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆