how to get the age by inserting the bithdate

13 posts Page 1 of 2
Contributors
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Hello guys ,


I am working on php nowadays so i thought i would share this little snippet with you all :D . So here goes

this calculates the age when inputting the birthdate.
Code: Select all
 function getAge( $p_
strDate ) {
 list($d,$m,$Y) = explode
("-",$p_strDate);
 return( date("md") < $m.$
d ? date("Y")-$Y-1 : date("Y")
-$Y );
 }

Day-Month-Year format. You can
change it by changing the order
of "list($d,$m,$Y)".
An example on using it:

Code: Select all
 <?php
 echo getAge("1-1-2000");

 function getAge( $p_
strDate ) {
 list($d,$m,$Y) = explode
("-",$p_strDate);
 return( date("md") < $m.$
d ? date("Y")-$Y-1 : date("Y")
-$Y );
 }
done !

Hope this helped you

thank you

edit - subject changed
Find my programs on Softpedia
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

is this post helped you ? Why i am asking because if you guys interested i can make more tutorials . So what ?
Find my programs on Softpedia
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

nice going :)
never new this :D thx.
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Something like this in VB?
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

How about this?
Code: Select all
Dim year As Integer = Today.Year - 1997
        Dim month As Integer = Today.Month - 9
        Dim day As Integer = Today.Day - 10

        If month < 1 Then
            year -= 1
            month += 12
        End If

        If day < 1 And month = 1 Or month = 3 Or month = 5 Or month = 7 Or month = 8 Or month = 10 Or month = 12 Then
            month -= 1
            day += 31
        ElseIf day < 1 And month = 4 Or month = 6 Or month = 9 Or month = 11 Then
            month -= 1
            day += 30
        ElseIf day < 1 And month = 2 Then
            month -= 1
            day += 28
        End If

        MsgBox("You are " & year & " years, " & month & " months, and " & day & " days old.")
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Or:
Code: Select all
        Dim birthdate As DateTime = New Date(1998, 3, 10)
        Dim totDays As Integer = (DateTime.Now - birthdate).TotalDays

        Dim years As Integer = totDays \ 365
        Dim days As Integer = totDays Mod 365
        Dim months As Integer = days \ 30
        days = days Mod 30

        MessageBox.Show(years.ToString() + " years, " + months.ToString() + " months, " + days.ToString() + " days old.")
The simplest would be
Code: Select all
MessageBox.Show(((DateTime.Now - Birthdate).Days / 365.0).ToString() + " years old.")
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

But that doesn't take into consideration months that have 31 or 28 days, does it?
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Nope, just for 30 day months, which would be somewhat the average of the months :)
Edit:
Code: Select all
Dim Age As DateTime = New DateTime((DateTime.Now - Birthday).Ticks)
MessageBox.Show(Age.Year.ToString() + " years, " + Age.Month.ToString() + " months, " + Age.Day.ToString() + " days.")
Last edited by MrAksel on Tue Jun 26, 2012 3:22 pm, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Oh ok. Well the code i posted takes care of 30-day, 31-day and 28-day months :D
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Edited :) Last post considers 31 & 28 days months
Edit: Nevermind, I realized its not working
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
13 posts Page 1 of 2
Return to “Help & Support”