DEV Community

Patrik Kiss
Patrik Kiss

Posted on

Get how much time has passed since given date in "...ago" format in PHP

Hi everyone,

In this post I'm just sharing a piece of code to get the time elapsed between two dates, like "12 seconds ago"/"4 hours ago" etc.
This is probably nothing special at all, most programmers know how to do this without any problem, but there might be someone who finds it useful.

This is also probably not the best way to do it, but with my current knowledge and experience, this is what I could do. I believe this is a fairly simple solution, but still works perfectly.

function timeElapsed($date){
        $months=array();
        for ($i=1; $i < 13; $i++) { 
            $month = date('F',mktime(0,0,0,$i));
            $months += [substr($month,0,3) => $i];
        }
        $date_year = date('Y', strtotime($date));//year of the date
        $date_month = date('m', strtotime($date));//month of the date
        $date_day = date('d', strtotime($date));//day of the date
        $date_hour = date('h', strtotime($date));//hour of the date
        $date_minute = date('i', strtotime($date));//minute of the date
        $current_year = date('Y');//current year(2019 in this case)

        //seconds passed between the given and current date
        $seconds_passed = round((time()-strtotime($date)),0);

        //minutes  passed between the given and current date
        $minutes_passed = round((time()-strtotime($date))/ 60,0);

        //hours passed between the given and current date
        $hours_passed = round((time()-strtotime($date))/ 3600,0);

        //days passed between the given and current date
        $days_passed = round((time()-strtotime($date))/ 86400,0);

        if($seconds_passed<60) echo $seconds_passed." second".($seconds_passed == (1) ? " " : "s")." ago";
        //outputs 1 second / 2-59 seconds ago

        else if($seconds_passed>=60 && $minutes_passed<60) echo $minutes_passed." minute".($minutes_passed == (1) ? " " : "s")." ago";
        //outputs 1 minute/ 2-59 minutes ago

        else if($minutes_passed>=60 && $hours_passed<24) echo $hours_passed." hour".($hours_passed == (1) ? " " : "s")." ago";
        //outputs 1 hour / 2-23 hours ago

        else if($hours_passed>=24 && $days_passed<2) echo "Yesterday at ".$date_hour.":".$date_minute;
        //outputs [Yesterday at 11:30] for example

        else{
            if($current_year!=$date_year){
                foreach($months as $month_name => $month_number){
                    if($month_number==$date_month){
                        echo $month_name." ".$date_day.", ".$date_year." ".$date_hour.":".$date_minute;
                        //echo $date_hour < (12) ? "AM" : "PM " ;
                        //outputs [Dec 11, 2018 11:32] for example
                    }
                }
            }
            else{
                foreach($months as $month_name => $month_number){
                    if($month_number==$date_month){
                        echo $month_name." ".$date_day.", ".$date_hour.":".$date_minute;
                        //echo $date_hour < (12) ? "AM" : "PM " ;
                        //outputs [Dec 11, 11:32] for example
                    }
                }
            }
        }
    }

Just in case:

$months=array();
for ($i=1; $i < 13; $i++) { 
    $month = date('F',mktime(0,0,0,$i));
    $months += [substr($month,0,3) => $i];
}

Is the same as manually creating the array like this

$months = array(
    "Jan"=>"01",
    "Feb"=>"02",
    "Mar"=>"03",
    "Apr"=>"04",
    "May"=>"05",
    "Jun"=>"06",
    "Jul"=>"07",
    "Aug"=>"08",
    "Sep"=>"09",
    "Oct"=>"10",
    "Nov"=>"11",
    "Dec"=>"12");

And finally you just call it like this

echo timeElapsed($yourDate);

Top comments (0)