473,396 Members | 1,832 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,396 software developers and data experts.

Date subtraction

Is there a php function to determine the difference between dates.

I'd like to take a birthdate and then determine how old a person is
dynamically and simply return the age in years.

TIA

Apr 11 '07 #1
5 6672
un1xg33k wrote:
Is there a php function to determine the difference between dates.
Yes: it's pronounced "minus" and written "-".
I'd like to take a birthdate and then determine how old a person is
dynamically and simply return the age in years.
<?php
$birthdate = strtotime('1 June 1980');
$now = time();

$diff_in_seconds = $now - $birthdate;
$diff_in_days = $diff_in_seconds / (24*60*60);
$diff_in_years = $diff_in_days / 365.25;

printf("I am %d years old!", (int)$diff_in_years);
?>

The above may misbehave on the day before/after/of someone's birthday due
to stupid bloody leap years, which are always annoying, but it should give
you a basic idea of how date calculations can be done.

If you're very concerned about them, it's not rocket science to counteract
the effect of leap years.

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

* = I'm getting there!
Apr 11 '07 #2
TY

Apr 11 '07 #3
OK, a bit more help.

As you can tell I'm on php expert and am hacking a page to add this
into it.

The birthday is stored in mysql as a date and shows up as 2000-11-02.

Do I need to do anything to be able to subtract that date from the
current date?

Also, how can I get a variable to show just the integer portion,
rather than doing it through a printf?
Apr 11 '07 #4
Ok, so the strtotime will take the date in that format and appears to
work. Now I just need the way to assign the integer to the variable.

Also what's the easiest way to put in something like <1 if the age is
less than one?

Apr 11 '07 #5
Got it working:

$now = time();
$birthday = strtotime($row[birthday]);
$age = (integer)(($now - $birthday) / 31557600);
if ($age=="0")
{
$age = "<1";
}

Any better way to do this?
Apr 11 '07 #6

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

Similar topics

5
by: gsv2com | last post by:
Maybe I'm tired, but I'm having a small problem with a date function I'm writing. Total noobish I know, but this is just going beyond me for some reason... What I want to happen is send a date...
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: sylvian stone | last post by:
Hi, I've used standard date functions in the past, but need to create something a little different, as I am working on an investment calculator. What I need to do is validate two dates, and...
3
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
11
by: Karl O. Pinc | last post by:
Hi, What's the best way to obtain the Julian day from a postgresql date? PostgreSQL 7.3.4 on i386-redhat-linux-gnu, compiled by GCC i386-redhat-linux-gcc (GCC) 3.2.2 20030222 (Red Hat Linux...
4
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
2
sumaiya
by: sumaiya | last post by:
how can i display date subtraction
5
by: dan | last post by:
I could find no documentation that the Date object has useful wrapping properties. For example, write(new Date(2007, 11 + 1, 1)) results in January 1st 2008. And write(new Date(2007, 0, 31 + 1))...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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,...
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
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...
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...

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.