doupu2722
2015-05-18 09:25
浏览 73
已采纳

如何检测当前日期是否大于datetime?

Actually I'd like to create a function which return me the date if the day is greater than one and it should return the time age like 1 hour ago.

I have done almost 80%, Time Ago functionality now I want to add one more feature.

function dateTimeExe ($time)
{

    $time = time() - $time; // to get the time since that moment

    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }

}

Please view output it may help to understand my question.

If day is less than 1 then it is working properly // 1 min ago or 1 hour ago Now I want to add if day is greater than 1 then it should return date in this format // 18 May 2015.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpa2143 2015-05-18 09:48
    已采纳

    Actually, I add if statement in my function and now its working

    if ($time>86400) {
            return date('d F Y');
        }else{
            foreach ($tokens as $unit => $text) {
                if ($time < $unit) continue;
                $numberOfUnits = floor($time / $unit);
                return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
            }
        }
    
    已采纳该答案
    打赏 评论
  • ds355020 2015-05-18 09:29

    What you can do is getting the timestamp from your datetime (http://php.net/manual/fr/class.datetime.php) thanks to getTimestamp() method and finally you can compare them with each other

    Assuming $date is a Datetime instance :

    $time = time() - $time->getTimestamp();
    
    打赏 评论

相关推荐 更多相似问题