473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2136
On Apr 17, 1:39 pm, JamesG <mailmeh...@gma il.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.operat ors.php#languag e.operators.pre cedence>

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*********@gma il.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...@gm ail.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...@gma il.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*********@gma il.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
1995
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 kevin
1
406
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 work for some reason. Here is the code: ' Create an appdomain Try Dim setup As AppDomainSetup = New AppDomainSetup setup.ApplicationBase = "C:\test" setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory setup.ApplicationName =...
11
6267
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 hours.it rounds up to 6 and not 5:30. I'm using this formula: Me.Hours = (DateDiff("h", Me.Start, Me.Stop)) Ex:
23
3282
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 to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
1
3442
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. Before: ----------
4
2278
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 dataSet updating lark when i realised that i think i am right back at square 1!!! Here's my scenario - i have a SQLDB and i retrieve all my data from that into a dataset and display this to a datagrid(WebForm). I have got this grid sorted and...
1
1318
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 client access to the ASP.NET applications on the Web server by completing the following steps: 1. Create the appropriate Domain Name System (DNS) entries for the ASP.NET
0
2837
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 java.util.*; import java.awt.*; /** * The Stock is the deck of unused cards. It is subtly different from the deck * itself in several ways. Firstly, it does not come into existence until after * the four cards have been extracted from the...
4
3552
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. Is this something that I can change with my code inside ASP.NET/C#.NET? And where? Does anyone know how to avoid this? It is very annoying and the website is not working as it should. People should be able to open new tabs/windows on my...
0
2760
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 DLPayDate I created the query in design view of access. Current Query: SELECT PaymentCalculator2.ID, PaymentCalculator2.RecDate, DateAdd("w",5+1,) AS DLPayDate, DateAdd("w",5,) AS DLPayDate2 FROM PaymentCalculator2 WHERE...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.