Warning: Undefined array key "show" in /home/shtechno/public_html/shivamkhare.in/wp-content/themes/shivam/header.php on line 13
Warning: Undefined variable $bodyClass in /home/shtechno/public_html/shivamkhare.in/wp-content/themes/shivam/header.php on line 50
class="post-template-default single single-post postid-217 single-format-standard vertical">
close
Contact

Human readable date format in PHP

In this blog, you’ll learn or get code for showing post date convert to human readable format in PHP or WordPress.

I would like to have dates on posts be formatted in a human way. The rules below are meant for things like press releases, events and news etc… The rules shouldn’t be used where a different format is expected, like in a publication citation for example.

Here are the rules. If any seem problematic or counter to your intuition, please let me know.

Example formatting (applicable timeframe)

Just Now (0 to 3 minutes ago)
3m ago (3 to 59 minutes ago)
1h ago (1 to 4 hours ago)
Today (>4 hours ago on same calendar date)
Yesterday (previous calendar date)
Sunday (past 7 days)
January 1 (>7 days ago within calendar year)
January 1, 2017 (previous calendar year)
January 2016 (before previous calendar year)
2013 (>5 calendar years)

Solution or Code below

For Showing the date firstly we have to pass a date through function or direct i’m working with function.

<?php 
echo human_readable_date('2020-03-17 07:00:00');
?>

Now add a function time_elapsed_string in your code. This is the function for return the time difference between current time and provided date time by us.

time_elapsed_string function we are using in main function human_readable_date for getting time elapsed.

function time_elapsed_string($datetime, $full = true) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    if (!$full) $string = array_slice($string, 0, 1);
    $deatarray =  $string ? implode(', ', $string) . ' ago' : 'just now';
    $detestring = explode(", ",$deatarray);
    return $detestring[0];
}

Through the time_elapsed_string function now we have a time elapsed string like 2 seconds ago, 9 minutes, 3 hours, 1 day , 2 weeks, 3 years etc..

Now we get time_elapsed_string in another function where we actually passing date time human_readable_date() and convert it to applicable timeframe format which is easy to read user.

function human_readable_date($date){
    $unixtimestamp = strtotime( $date );
    $ytime=strtotime($date);
    $prevyear = date("Y",strtotime("-1 year"));
    $fiveyearprev = date("Y",strtotime("-5 year"));
    $currentyr = date("Y");
    $dateyr =  date("Y",$ytime);
    $d_diff = time_elapsed_string($date);
    $d_diff_array = explode(" ",$d_diff);  
    if($d_diff_array[1] == 'seconds' ||  $d_diff_array[1] == 'second' || $d_diff_array[1] == 'minute'){
        return $showtime = 'Just Now';
    }elseif($d_diff_array[1] == 'minutes'){
        if($d_diff_array[0] < 4){
            return $showtime = 'Just Now';
        }else{
            return $showtime = $d_diff_array[0].'m ago';
        }
    }elseif($d_diff_array[1] == 'hour'){
        return $showtime = '1h ago';
    }elseif($d_diff_array[1] == 'hours'){
        if($d_diff_array[0] < 4){
            return $showtime = $d_diff_array[0].'h ago';
        }else{
            $current = strtotime(date("Y-m-d"));
            $date    = strtotime($date);
            $datediff = $date - $current;
            $difference = floor($datediff/(60*60*24));
            if($difference==0)
            {
                return 'Today';
            }else{
                return 'Yesterday';
            }
        }
    }elseif($d_diff_array[1] == 'day'){
        return 'Yesterday';
	}elseif($d_diff_array[1] == 'days'){
        return date_i18n( "l", $unixtimestamp );
	}elseif($d_diff_array[1] == 'week' || $d_diff_array[1] == 'weeks' || $d_diff_array[1] == 'month'){
        return date_i18n( "F j", $unixtimestamp );
	}elseif($d_diff_array[1] == 'months'){
        if($currentyr == $dateyr){
            return date_i18n( "F j", $unixtimestamp );
		}else{
            return date_i18n( "F j, Y", $unixtimestamp );
		}
	}elseif($d_diff_array[1] == 'year' || $d_diff_array[1] == 'years'){
        if($fiveyearprev >= $dateyr){
            return $dateyr;
		}elseif($prevyear == $dateyr){
            return date_i18n( "F j, Y", $unixtimestamp );
		}else{
            return date_i18n( "F Y", $unixtimestamp ); 
		}
	}else{
        return $d_diff;
  	}
    
}

This code you can use as it is in your website.

If you have any query/problem or getting error in the cod, just send me a mail shivamkhare02@gmail.com.