If Time is Less than 24 hours Show Hours/Minutes/Seconds...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

If Time is Less than 24 hours Show Hours/Minutes/Seconds...

Post 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?
User avatar
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

Post 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.
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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.
User avatar
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

Post 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.
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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:

Code: Select all

the_time('F jS, Y');
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.
User avatar
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

Post by Jonah Bron »

Try the_time('U)
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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."
User avatar
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

Post 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');
}
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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.
User avatar
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

Post 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
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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.
User avatar
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

Post 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');
}
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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 :D
User avatar
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

Post by Jonah Bron »

Sure. It's fun (for me) solving simple problems like this.
ccmovies
Forum Newbie
Posts: 15
Joined: Mon Oct 11, 2010 9:14 pm

Re: If Time is Less than 24 hours Show Hours/Minutes/Seconds

Post 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.
Post Reply