If Time is Less than 24 hours Show Hours/Minutes/Seconds...
Moderator: General Moderators
If Time is Less than 24 hours Show Hours/Minutes/Seconds...
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?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
You could do that with a simple set of if...elseif statements. Here's an example:
Note: I did not test this; I may have gotten the time unit conversions wrong.
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);
}Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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:Jonah Bron wrote:You could do that with a simple set of if...elseif statements. Here's an example:Note: I did not test this; I may have gotten the time unit conversions wrong.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); }
Code: Select all
the_time(
//PHP date letters go here
);
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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
For wordpress, when you want to get the date you use the code I mentioned: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.
Code: Select all
the_time('F jS, Y');
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);
}
I am sorry if I don't know what I am talking about, I am relatively new to Wordpress theme design and PHP.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Try the_time('U)
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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."Jonah Bron wrote:Try the_time('U)
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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
Nope, it did the same thing "1286839068October 11th, 2010" My website is http://connorcrosby.net/blog if it helps at all.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'); }
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Oops! the_time outputs the time, it doesn't return it. Now it should totally work:
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
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');
}
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
It works! Only one problem, the time (hours, minutes) are shown weird. For example: 4.8380555555556 hour(s) ago.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'); }
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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
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 
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
Sure. It's fun (for me) solving simple problems like this.
Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds
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.