473,408 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
6 2118
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kevin | last post by:
meta refresh tag is failing , it is supposed to go to the entry1.htm site which is the flash swf file but it doesn't check code please http://members.optusnet.com.au/~kevindauth/ thanks...
1
by: LilleSkutt | last post by:
I am trying to create and instantiate a class into a created domain, so that I can unload the domain and replace the class (assembly .dll) while the main application is running, but I can't get it to...
11
by: christian | last post by:
Hi all, I'm creating a TimeSheet Database, I need to calculate how many hours the Employee works.The problem is that when they enter the time, it doesn't calculate the minutes, it just calculate...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
by: Chua Wen Ching | last post by:
Hi there, I have some problems when reading XML file. 1. First this, is what i did, cause i can't seem to read "sub elements or tags" values, so i place those values into attributes like this....
4
by: steroche | last post by:
I would REALLY appreciate help please please please! Im sure it is probably blindingly obvious to most of you but I am totally in the dark here!I am lost - i thought i had finally figured out this...
1
by: Andrew | last post by:
Hi, friends, I am following the "MS IIS 6.0 Deployment Guide" to deploy my asp.net application. However, I got completely lost in the session "Enabling Client Access IIS 6.0". It says: Enable...
0
by: Limpor | last post by:
Hi, I’m working on a solitaire game as my course assignment, and I am having trouble to dealing with Stock class, which consists of an upturned top card plus deck. The code for Stock class: import...
4
by: R Reyes | last post by:
Whenever I right-click on "Open in New Tab/Window" my website ALWAYS opens in the same window/tab. This is most likely a programming issue because when I visit other websites it works just fine. ...
0
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.