0

I have a blog in my site. In blog listing, I want to show date and time. I get the published post date with the_time(). But the getting time is a tricky part.

I don't want to show posted time e.g. 1:30 PM

I want to show the time duration of a post ... I mean like this

53 minutes ago or 4 hours ago or 2 days ago or 5 months ago or 1 year ago

I first try this code get_post_time('U', $post->ID) or get_post_time('U', true) but it gives me the time stamp, Like this 1486128075

And also try this get_the_time() but it gives me 1:00 PM

Hope you understand my question

deemi-D-nadeem
  • 2,343
  • 3
  • 30
  • 71

1 Answers1

2

Add this code in your functions.php

function ash_relative_time() { 
$post_date = get_the_time('U');
$delta = time() - $post_date;
if ( $delta < 60 ) {
    echo 'Just Now';
}
elseif ($delta > 60 && $delta < 120){
    echo '1 minute ago';
}
elseif ($delta > 120 && $delta < (60*60)){
    echo strval(round(($delta/60),0)), ' minutes ago';
}
elseif ($delta > (60*60) && $delta < (120*60)){
    echo 'About an hour ago';
}
elseif ($delta > (120*60) && $delta < (24*60*60)){
    echo strval(round(($delta/3600),0)), ' hours ago';
}
else {
    echo the_time('j\<\s\u\p\>S\<\/\s\u\p\> M y g:i a');
}}

and then use echo ash_relative_time(); to print it.