473,790 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining an age

Hi All,

I am wondering what the best way to determine an age of something/somone is.
For instance, let's say I have two dates:

1985-07-19
2003-11-30

Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable length,
or if I do measure months I will simply say that a month is 30 days.

So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of seconds
from one date to the other. This is fine for small date ranges. For
instance, let's say I had these dates:

1985-07-19
1985-11-15

Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.

But things get complicated once the date range gets large enough, because I
now have to worry about leap years. Every 4 years we have an extra 86400
seconds in a year. So how do I handle this case? If I want to know the
number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for leap
years, where there is an extra day?

I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?

Thanks for any pointers!

Sincerely,
-Josh
Jul 17 '05 #1
8 1956
FUT is now set to comp.lang.php, sorry I missed that on the initial posting.

"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:pD2Wd.7775 1$wc.33781@trnd dc07...
Hi All,

I am wondering what the best way to determine an age of something/somone
is. For instance, let's say I have two dates:

1985-07-19
2003-11-30

Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable
length, or if I do measure months I will simply say that a month is 30
days.

So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of
seconds from one date to the other. This is fine for small date ranges.
For instance, let's say I had these dates:

1985-07-19
1985-11-15

Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.

But things get complicated once the date range gets large enough, because
I now have to worry about leap years. Every 4 years we have an extra
86400 seconds in a year. So how do I handle this case? If I want to know
the number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for
leap years, where there is an extra day?

I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?

Thanks for any pointers!

Sincerely,
-Josh

Jul 17 '05 #2

"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:pD2Wd.7775 1$wc.33781@trnd dc07...
Hi All,

I am wondering what the best way to determine an age of something/somone is. For instance, let's say I have two dates:

1985-07-19
2003-11-30

Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable length, or if I do measure months I will simply say that a month is 30 days.

So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of seconds from one date to the other. This is fine for small date ranges. For
instance, let's say I had these dates:

1985-07-19
1985-11-15

Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.

But things get complicated once the date range gets large enough, because I now have to worry about leap years. Every 4 years we have an extra 86400
seconds in a year. So how do I handle this case? If I want to know the
number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for leap years, where there is an extra day?

I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?

Thanks for any pointers!

Sincerely,
-Josh


Calculate the way a human would.

First, figure out when was the person's last birthday. If the person was
born in 1985-07-19, then he last celebrated his birthday on 2004-07-19.
Subtract the Unix time of that from the current time and divide by the
number of seconds in a day to get the number of days old.

Subtract the birth year from the current year to get the number of years
old. If the person hasn't celebrated his birthday yet this year, subtract
one.
Jul 17 '05 #3
.oO(Chung Leong)
Calculate the way a human would.

First, figure out when was the person's last birthday. If the person was
born in 1985-07-19, then he last celebrated his birthday on 2004-07-19.
Subtract the Unix time of that from the current time and divide by the
number of seconds in a day to get the number of days old.


What if the person was born on 1965-07-19?

I would avoid Unix timestamps because of their range restriction and use
some library for date calculations (IIRC there's something in PEAR).
Another way would be to make use of PHP's calendar functions, but these
might not be compiled in by default.

Micha
Jul 17 '05 #4
Hey,

That really sucks huh? And when 2038 (I think) rolls around, the bits will
be maxed out! Amazing how they can create such a solid OS and not even
consider these things.

Anyway, I found a solution at php.net.
Check it out at http://www.php.net/mktime in the comments, or read below!!

Thanks,
Mike

----------------------------------------------------------
If you want to calculate dates before 1970 and your OS doesn't support it
(Windows and Linux), you can easily write your own wrapper to mktime to do
the calculation. The idea is to find a date range in the 1970-2037 time
frame that is the same as your year prior to 1970 (both years start on the
same day of the week and leap years are the same), then add that number of
years to your year value before calling mktime() and then subtract the
corresponding number of seconds from the mktime() result (including the
appropriate number of leap seconds). This way old guys like me (born in
1963) can get a valid number back from mktime() and we can figure out how
old we are in seconds. :-) You can go all the way back to 1902 if you
want.

The mapping is as follows:
1902-1951 = 1986-2035
1952-1969 = 1980-1997
By matching starting day of week for the year and leap years then we should
calculate daylight time correctly (assuming that we have the right rules
set). There's a special hack in the code to turn off daylight time prior to
1942 for date().

Here's the code that we're using now:

//
// Replacement for mktime that does dates before 1970
//
function MakeTime()
{
$objArgs = func_get_args() ;
$nCount = count($objArgs) ;
if ($nCount < 7)
{
$objDate = getdate();
if ($nCount < 1)
$objArgs[] = $objDate["hours"];
if ($nCount < 2)
$objArgs[] = $objDate["minutes"];
if ($nCount < 3)
$objArgs[] = $objDate["seconds"];
if ($nCount < 4)
$objArgs[] = $objDate["mon"];
if ($nCount < 5)
$objArgs[] = $objDate["mday"];
if ($nCount < 6)
$objArgs[] = $objDate["year"];
if ($nCount < 7)
$objArgs[] = -1;
}
$nYear = $objArgs[5];
$nOffset = 0;
if ($nYear < 1970)
{
if ($nYear < 1902)
return 0;
else if ($nYear < 1952)
{
$nOffset = -2650838400;
$objArgs[5] += 84;
// Apparently dates before 1942 were never DST
if ($nYear < 1942)
$objArgs[6] = 0;
}
else
{
$nOffset = -883612800;
$objArgs[5] += 28;
}
}

return call_user_func_ array("mktime", $objArgs) + $nOffset;
}

In Linux, the values returned will work just fine with date() and I believe
that strftime() should work too, but Windows doesn't like the negative
values at all. We had to write a replacement for strtotime since it appears
to call mktime() to come up with the final result.

I hope that this helps someone.

- todd

"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:pD2Wd.7775 1$wc.33781@trnd dc07...
Hi All,

I am wondering what the best way to determine an age of something/somone
is. For instance, let's say I have two dates:

1985-07-19
2003-11-30

Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable
length, or if I do measure months I will simply say that a month is 30
days.

So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of
seconds from one date to the other. This is fine for small date ranges.
For instance, let's say I had these dates:

1985-07-19
1985-11-15

Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.

But things get complicated once the date range gets large enough, because
I now have to worry about leap years. Every 4 years we have an extra
86400 seconds in a year. So how do I handle this case? If I want to know
the number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for
leap years, where there is an extra day?

I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?

Thanks for any pointers!

Sincerely,
-Josh

Jul 17 '05 #5
"Michael Fesser" <ne*****@gmx.ne t> wrote in message
news:a3******** *************** *********@4ax.c om...
.oO(Chung Leong)
Calculate the way a human would.

First, figure out when was the person's last birthday. If the person was
born in 1985-07-19, then he last celebrated his birthday on 2004-07-19.
Subtract the Unix time of that from the current time and divide by the
number of seconds in a day to get the number of days old.


What if the person was born on 1965-07-19?


His birthday would still be July 19th. That doesn't change anything.
Jul 17 '05 #6
.oO(Chung Leong)
"Michael Fesser" <ne*****@gmx.ne t> wrote
What if the person was born on 1965-07-19?


His birthday would still be July 19th. That doesn't change anything.


The age calculation using Unix timestamps will fail.

Micha
Jul 17 '05 #7

"Michael Fesser" <ne*****@gmx.ne t> wrote in message
news:vh******** *************** *********@4ax.c om...
.oO(Chung Leong)
"Michael Fesser" <ne*****@gmx.ne t> wrote
What if the person was born on 1965-07-19?


His birthday would still be July 19th. That doesn't change anything.


The age calculation using Unix timestamps will fail.


Not for another three decades. Read the original message and my response
again.
Jul 17 '05 #8
.oO(Chung Leong)
Not for another three decades. Read the original message and my response
again.


Got it. Sorry, my fault. Read too fast.

Micha
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1354
by: D & J Gilchrist | last post by:
Hi Is there any way of determining the width and height of a graphic in pixels (.jpg) and reporting this? Also, maybe following the above, can a picture-box position be automatically moved to place the graphic's right-hand edge at the right-hand edge of a form, no matter what size the graphic? (Using VB5)
12
3667
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work either in "online" or "offline" mode (it keeps a cache of downloaded info, so it can work without a connection if needed). The basic problem is this: it downloads info (RSS feeds) from a variety of sources. Any one (or more) of these could...
0
1242
by: Stephen Nesbitt | last post by:
All: Here's my implementation problem. I have a base class which has the responsibility for providing entry into the logging system. Part of the class responsibility is to ensure that lagger names are consistent. For all intents and purposes this class should be considered abstract and will always be subclassed. What I want to do is the following: - allow the logger name to set explicitly. I've accomplished this by
2
5425
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as follows: ****************************************** struct ST_MSG { int iType; char aData; }
1
26973
by: Simon Wigzell | last post by:
I am adapting a javascript pulldown menu system to my dynamic website generator - the arrays that hold the menu items information are read from a database and will be different for different users of my system. Unfortunately the javascript menu system requires that each menu cell's width be dimensioned. if I don't hard code a large enough value then long menu item names are trimmed, besides the fact that it then looks ugly for most menu...
2
2254
by: Phil Galey | last post by:
In VB.NET I find the IO object very handy in replacing most of the functionality of the FileSystemObject. One exception, however, is in determining the size of a file. How can you determine the size of a file in VB.NET without adding the FileSystemObject to your project?
12
7888
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
4
1436
by: - Dazed | last post by:
If a user is using a combination of Win98 & any version of MSIE, I want to display a message. The best that I can do is the following: <? if ((strstr (getenv('HTTP_USER_AGENT'), 'MSIE')) && (strstr (getenv('HTTP_USER_AGENT'), '98'))) { echo"blah,blah,blah\n"; } ?>
3
1628
by: Fred Nelson | last post by:
I'm devloping a Web Application in VB.NET. In my web.config file I have specified that untrapped errors are to be sent to the page "errorpage.aspx". This is working fine - if an untrapped error occurs the application is indeed routed to this page. On this page I would like to determine the cause of the error and either log it in a file or send it to me via e-mail. I am able to determine the page that sent me there from the URL...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
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
9021
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
7530
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
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4094
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
2909
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.