Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

php - function that formats the time difference

I am trying to create a function that compares a date with the current time, and returns a nicely formatted string.
I have written some code in haste and it works, but I am trying to find a more efficient way of doing it. Here is my code:

    function _formatDate($dateStr)
    {
    $timestr = "";
    $t= time() - strtotime($dateStr);
    if($t < 60) {
        $timestr = "{$t} seconds ago";
    }
    elseif ($t <120) {
        $timestr = "about a minute ago";
    }
    elseif ($t < 3600) {
        $minute = floor($t/60);
        $timestr = "{$minute} minutes ago";
    }
    elseif ($t < 7200) {
        $timestr = " about an hour ago";
    }
    elseif ($t < 86400) {
        $hour = floor($t/3600);
        $timestr = "{$hour} hours ago";
    }
    elseif ($t < 172800) {
        $timestr = "a day ago";
    }
    elseif ($t < 2592000) {
        $day = floor($t/86400);
        $timestr = "{$day} days ago";
    }
    elseif ($t < 5184000){
        $timestr = "about a month ago";
    }
    else {
        $month = floor($t/2592000);
        $timestr = "{$month} months ago";
    }
    return $timestr;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some code I use and it's never failed, just put the Unix timestamp in, if you use a second argument in the function that can be the "to" condition

echo timeDiffrence('1300392875');

function timeDiffrence($from, $to = null){
    $to = (($to === null) ? (time()) : ($to));
    $to = ((is_int($to)) ? ($to) : (strtotime($to)));
    $from = ((is_int($from)) ? ($from) : (strtotime($from)));
    $units = array
    (
    "y"   => 29030400, // seconds in a year   (12 months)
    "month"  => 2419200,  // seconds in a month  (4 weeks)
    "w"   => 604800,   // seconds in a week   (7 days)
    "d"    => 86400,    // seconds in a day    (24 hours)
    "h"   => 3600,     // seconds in an hour  (60 minutes)
    "m" => 60,       // seconds in a minute (60 seconds)
    "s" => 1         // 1 second
    );
    $diff = abs($from - $to);
    $suffix = (($from > $to) ? ("from now") : ("ago"));
    foreach($units as $unit => $mult)
    if($diff >= $mult)
    {
        //$and = (($mult != 1) ? ("") : ("and "));
        $output .= "".$and.intval($diff / $mult)."".$unit.((intval($diff / $mult) == 1) ? ("") : (""));
        $diff -= intval($diff / $mult) * $mult;
    }
    $output .= " ".$suffix;
    $output = substr($output, strlen(""));
    if($output =='go' || $output ==' ago'){$output = 'A few secs ago';}
    return $output;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...