×

php Time ago function

php Time ago function

<?php 
    function get_time_ago($time){
        $time_difference=time()-$time;
        if ($time_difference < 1) { return 'less than 1 second ago';}
        $condition=array(12*30*24*60*60 => 'year',
                        30*24*60*60     => 'month',
                        24*60*60        => 'day',
                        60*60           => 'hour',
                        60              => 'munite',
                        1               => 'second'
        );
        foreach($condition as $sec => $str)
        {
            $d=$time_difference /$sec;
            if ($d >= 1) {
                $t=round($d);
                return 'about '.$t.' '.$str. ($t > 1 ? 's' :'').' ago';
            }
            
        }
 }
    
        // Timezone set first
    echo get_time_ago(strtotime("2021-12-16 12:50:00"));
 ?>

| Share | Facebook | Twitter | Whatsapp | Copy Link
SR Tech Limited logo

SR Tech Limited is a blog where I post blogs related to HTML CSS JavaScript & PHP along with creative coding stuff and free source code files.


Add Comments

Previous Comments

Unknown User 02-Aug-2023
fine
Go to top // Link copy to clipboard