By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,474 Members | 3,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,474 IT Pros & Developers. It's quick & easy.

Help me fix my "x minutes ago" time script please :)

P: n/a
I'm trying to convert a unix timestamp to a readable time, such as 4
hours ago, or 25 minutes ago.
My code is below:

function ago($ts) {
/* time difference */
$ts = (time() - $ts);

if($ts < 60) {
/* <1 minute */
return $ts . " seconds ago";
} elseif($ts < 60 * 60) {
/* <1 hour */
return floor($ts / 60) . " minutes ago";
} elseif ($ts < 60 * 60 * 2) {
/* <2 hour */
return "1 hour ago";
} elseif ($ts < 60 * 60 * 24) {
/* <24 hours = 1 day */
return floor($ts / 60 * 60) . " hours ago";
} elseif ($ts < 60 * 60 * 24 * 2) {
/* <2 days */
return "1 day ago";
} elseif ($ts < 60 * 60 * 24 * 7) {
/* <7 days = 1 week */
return floor($ts / 60 * 60 * 24) . " days ago";
} elseif ($ts < 60 * 60 * 24 * 30.5) {
/* <30.5 days ~ 1 month */
return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";
}
}

Everything seems to work okay, except the "hours ago" case. A time 2
hours ago will output "9692 hours ago", or a time 18 hours ago will
read "9692 hours ago"!

Anyone help here? Thanks in advance

Apr 17 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Apr 17, 1:39 pm, JamesG <mailmeh...@gmail.comwrote:
I'm trying to convert a unix timestamp to a readable time, such as 4
hours ago, or 25 minutes ago.
My code is below:

function ago($ts) {
/* time difference */
$ts = (time() - $ts);

if($ts < 60) {
/* <1 minute */
return $ts . " seconds ago";
} elseif($ts < 60 * 60) {
/* <1 hour */
return floor($ts / 60) . " minutes ago";
} elseif ($ts < 60 * 60 * 2) {
/* <2 hour */
return "1 hour ago";
} elseif ($ts < 60 * 60 * 24) {
/* <24 hours = 1 day */
return floor($ts / 60 * 60) . " hours ago";
} elseif ($ts < 60 * 60 * 24 * 2) {
/* <2 days */
return "1 day ago";
} elseif ($ts < 60 * 60 * 24 * 7) {
/* <7 days = 1 week */
return floor($ts / 60 * 60 * 24) . " days ago";
} elseif ($ts < 60 * 60 * 24 * 30.5) {
/* <30.5 days ~ 1 month */
return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";
}

}

Everything seems to work okay, except the "hours ago" case. A time 2
hours ago will output "9692 hours ago", or a time 18 hours ago will
read "9692 hours ago"!

Anyone help here? Thanks in advance
It should be $ts / (60 * 60). Read about Operator Precedence:

<http://www.php.net/manual/en/
language.operators.php#language.operators.preceden ce>

Apr 17 '07 #2

P: n/a
JamesG kirjoitti:
I'm trying to convert a unix timestamp to a readable time, such as 4
hours ago, or 25 minutes ago.
My code is below:

function ago($ts) {
/* time difference */
$ts = (time() - $ts);

if($ts < 60) {
/* <1 minute */
return $ts . " seconds ago";
} elseif($ts < 60 * 60) {
/* <1 hour */
return floor($ts / 60) . " minutes ago";
} elseif ($ts < 60 * 60 * 2) {
/* <2 hour */
return "1 hour ago";
} elseif ($ts < 60 * 60 * 24) {
/* <24 hours = 1 day */
return floor($ts / 60 * 60) . " hours ago";
Parenthesis missing here. First you divide by 60, then multiply by 60,
resulting in the same number you started with. This is the same as $ts *
60 / 60 which is the same as $ts. You're getting seconds instead of
hours. Put some parenthesis there to help php understund what you mean:
$ts / (60 * 60) // now it's executed in the correct order
} elseif ($ts < 60 * 60 * 24 * 2) {
/* <2 days */
return "1 day ago";
} elseif ($ts < 60 * 60 * 24 * 7) {
/* <7 days = 1 week */
return floor($ts / 60 * 60 * 24) . " days ago";
Same here: (60 * 60 * 24)
} elseif ($ts < 60 * 60 * 24 * 30.5) {
/* <30.5 days ~ 1 month */
return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
and here: (60 * 60 * 24 * 7)
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
and here
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";
.... and also here: (60 * 60 * 24 * 365)
}
}

Everything seems to work okay, except the "hours ago" case. A time 2
hours ago will output "9692 hours ago", or a time 18 hours ago will
read "9692 hours ago"!
If it seemd to work okay then perhaps you hadn't tested the last ones,
cos they seem to suffer from the same problem as the "x hours" case.

A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
only correct syntax is "else if" with a space between, if you ask me! :D
(Sure, elseif *is* valid code but it's just disgusting!)

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Apr 17 '07 #3

P: n/a
On Apr 17, 7:37 pm, Rami Elomaa <rami.elo...@gmail.comwrote:
JamesG kirjoitti:
I'm trying to convert a unix timestamp to a readable time, such as 4
hours ago, or 25 minutes ago.
My code is below:
function ago($ts) {
/* time difference */
$ts = (time() - $ts);
if($ts < 60) {
/* <1 minute */
return $ts . " seconds ago";
} elseif($ts < 60 * 60) {
/* <1 hour */
return floor($ts / 60) . " minutes ago";
} elseif ($ts < 60 * 60 * 2) {
/* <2 hour */
return "1 hour ago";
} elseif ($ts < 60 * 60 * 24) {
/* <24 hours = 1 day */
return floor($ts / 60 * 60) . " hours ago";

Parenthesis missing here. First you divide by 60, then multiply by 60,
resulting in the same number you started with. This is the same as $ts *
60 / 60 which is the same as $ts. You're getting seconds instead of
hours. Put some parenthesis there to help php understund what you mean:
$ts / (60 * 60) // now it's executed in the correct order
} elseif ($ts < 60 * 60 * 24 * 2) {
/* <2 days */
return "1 day ago";
} elseif ($ts < 60 * 60 * 24 * 7) {
/* <7 days = 1 week */
return floor($ts / 60 * 60 * 24) . " days ago";

Same here: (60 * 60 * 24)
} elseif ($ts < 60 * 60 * 24 * 30.5) {
/* <30.5 days ~ 1 month */
return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";

and here: (60 * 60 * 24 * 7)
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";

and here
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";

... and also here: (60 * 60 * 24 * 365)
}
}
Everything seems to work okay, except the "hours ago" case. A time 2
hours ago will output "9692 hours ago", or a time 18 hours ago will
read "9692 hours ago"!

If it seemd to work okay then perhaps you hadn't tested the last ones,
cos they seem to suffer from the same problem as the "x hours" case.

A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
only correct syntax is "else if" with a space between, if you ask me! :D
(Sure, elseif *is* valid code but it's just disgusting!)

--
Rami.Elo...@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Thanks so much for correcting the silly mistake.
I also hate nested elseifs but in this case it's the only way to do it
I think-- I considered a switch statement but that isn't useful in
this instance.

Apr 17 '07 #4

P: n/a
JamesG kirjoitti:
I also hate nested elseifs but in this case it's the only way to do it
You kinda missed my point. I have nothing against else if's as they are,
just the way of writing elseif together, without a space between them
looks so stupid. As in "elseif" instead of "else if"... But that was
really not important at all... :)

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Apr 17 '07 #5

P: n/a
JamesG wrote:
>
Thanks so much for correcting the silly mistake.
I also hate nested elseifs but in this case it's the only way to do it
I think-- I considered a switch statement but that isn't useful in
this instance.
you actually don't need else's here, because you return in every branch.
Just use ifs:

if($ts < 60)
return $ts . " seconds ago";
if($ts < (60 * 60))
return intval($ts / 60) . " minutes ago";

etc
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Apr 17 '07 #6

P: n/a
Rami Elomaa wrote:
A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
only correct syntax is "else if" with a space between, if you ask me! :D
(Sure, elseif *is* valid code but it's just disgusting!)
I often use "elseif" -- before I started using PHP my main scripting
language was Perl, which has "elsif", so switching to "elseif" was only a
small learning curve.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.