Page 1 of 2
If Time is Less than 24 hours Show Hours/Minutes/Seconds...
Posted: Mon Oct 11, 2010 9:42 pm
by ccmovies
I am making a Wordpress theme and I want to display the date like Twitter does. If the time is less than 24 hours, then show how many hours or minutes it was. If it was less than a minute, show "a moment ago." If it is greater than 24 hours, show the date (for example, October 11th, 2010). How can I do this with PHP?
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:03 pm
by Jonah Bron
You could do that with a simple set of if...elseif statements. Here's an example:
Code: Select all
$now = time();
$then = // get that time in the form of a timestamp;
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
echo date('F jS, Y', $then);
}
Note: I did not test this; I may have gotten the time unit conversions wrong.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:24 pm
by ccmovies
Jonah Bron wrote:You could do that with a simple set of if...elseif statements. Here's an example:
Code: Select all
$now = time();
$then = // get that time in the form of a timestamp;
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
echo date('F jS, Y', $then);
}
Note: I did not test this; I may have gotten the time unit conversions wrong.
I am not sure what to do exactly with this code. I put it in where I would put the regular code which is this:
Code: Select all
the_time(
//PHP date letters go here
);
however it just shows the date it was published and the time.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:26 pm
by Jonah Bron
You need to put the date of the post into the $now variable, as per the comment. If you don't know how to do that, you'll need to post more of your code. We need to see where to get the date of the post.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:34 pm
by ccmovies
Jonah Bron wrote:You need to put the date of the post into the $now variable, as per the comment. If you don't know how to do that, you'll need to post more of your code. We need to see where to get the date of the post.
For wordpress, when you want to get the date you use the code I mentioned:
So, I guessed that I was suppose to do this:
Code: Select all
$now = time();
$then = the_time('F jS, Y');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
echo the_time('F jS, Y', $then);
}
However, that just results in repeating the date twice (without a space in between). If I make
$then = the_time(); it displays the time and the date (again, with no space in between.
I am sorry if I don't know what I am talking about, I am relatively new to Wordpress theme design and PHP.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:39 pm
by Jonah Bron
Try the_time('U)
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:41 pm
by ccmovies
Jonah Bron wrote:Try the_time('U)
I wasn't sure where you wanted me to try (either $time or at the bottom) so I tried both and they both resulted in the same thing. A long list of numbers such as "1286839068."
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:47 pm
by Jonah Bron
Code: Select all
$now = time();
$then = the_time('U');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
echo the_time('F jS, Y');
}
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:52 pm
by ccmovies
Jonah Bron wrote:Code: Select all
$now = time();
$then = the_time('U');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
echo the_time('F jS, Y');
}
Nope, it did the same thing "1286839068October 11th, 2010" My website is
http://connorcrosby.net/blog if it helps at all.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 10:59 pm
by Jonah Bron
Oops! the_time outputs the time, it doesn't return it. Now it should totally work:
Code: Select all
$now = time();
$then = get_the_time('U');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
the_time('F jS, Y');
}
Just for your education, get_the_time and the_time are functions created by Wordpress. get_the_time() simply returns the time of the post. The syntax for formatting the date is the same as the built-in date() function.
http://php.net/date
the_time() does the same thing, but it outputs it instead of returning it.
http://codex.wordpress.org/Function_Ref ... t_the_time
http://codex.wordpress.org/Function_Reference/the_time
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 11:08 pm
by ccmovies
Jonah Bron wrote:Oops! the_time outputs the time, it doesn't return it. Now it should totally work:
Code: Select all
$now = time();
$then = get_the_time('U');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval($diff / 60) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval($diff / (60 * 60)) . ' hour(s) ago';
} else {
the_time('F jS, Y');
}
It works! Only one problem, the time (hours, minutes) are shown weird. For example: 4.8380555555556 hour(s) ago.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 11:13 pm
by Jonah Bron
This should work properly. I just used round() to take of the decimal points.
Code: Select all
$now = time();
$then = get_the_time('U');
$diff = $now - $then;
if ($diff < 60) {
echo 'a moment ago';
} elseif ($diff < (60*60)) {
echo strval(round($diff / 60)) . ' minute(s) ago';
} elseif ($diff < (60*60*24)) {
echo strval(round($diff / (60 * 60))) . ' hour(s) ago';
} else {
the_time('F jS, Y');
}
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 11:16 pm
by ccmovies
YEAH! Thank you so much, you don't know how long I have been trying to figure out how to do that. I really appreciate it, thank you

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 11:18 pm
by Jonah Bron
Sure. It's fun (for me) solving simple problems like this.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Posted: Mon Oct 11, 2010 11:24 pm
by ccmovies
Oh no, one more problem. The time is off by about 4 hours. I just did a test post to see "a moment ago" however it said 4 hours ago. Also, the one I posted an hour ago says 5 hours ago.