Connecting Tech Pros Worldwide Help | Site Map

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

JamesG
Guest
 
Posts: n/a
#1: Apr 17 '07
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

ZeldorBlat
Guest
 
Posts: n/a
#2: Apr 17 '07

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


On Apr 17, 1:39 pm, JamesG <mailmeh...@gmail.comwrote:
Quote:
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>

Rami Elomaa
Guest
 
Posts: n/a
#3: Apr 17 '07

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


JamesG kirjoitti:
Quote:
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
Quote:
} 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)
Quote:
} 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)
Quote:
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
and here
Quote:
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";
.... and also here: (60 * 60 * 24 * 365)
Quote:
}
}
>
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.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
JamesG
Guest
 
Posts: n/a
#4: Apr 17 '07

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


On Apr 17, 7:37 pm, Rami Elomaa <rami.elo...@gmail.comwrote:
Quote:
JamesG kirjoitti:
>
>
>
Quote:
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:
>
Quote:
function ago($ts) {
/* time difference */
$ts = (time() - $ts);
>
Quote:
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
>
Quote:
} 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)
>
Quote:
} 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)
>
Quote:
} elseif ($ts < 60 * 60 * 24 * 365) {
/* <365 days = 1 year */
return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
>
and here
>
Quote:
} else {
/* more than 1 year */
return floor($ts / 60 * 60 * 24 * 365) . " years ago";
>
... and also here: (60 * 60 * 24 * 365)
>
Quote:
}
}
>
Quote:
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.



Rami Elomaa
Guest
 
Posts: n/a
#5: Apr 17 '07

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


JamesG kirjoitti:
Quote:
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... :)

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
gosha bine
Guest
 
Posts: n/a
#6: Apr 17 '07

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


JamesG wrote:
Quote:
>
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
Toby A Inkster
Guest
 
Posts: n/a
#7: Apr 18 '07

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


Rami Elomaa wrote:
Quote:
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!
Closed Thread