arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Calculate your Age with full detailes by Date of Birth (PHP)
#1
Information 
Hello everybody, so iam going to show you how to calculate your Age with full detailes (Y/M/H/m/s/ms/ns/ms) in PHP by Date of Birth.

If you Wanna Calculate your real Age in numeric format:
You Have to subtract date('year') from date('Y') and insert on the last date function's timestamp argument strtotime('Year-Month-Day')
PHP Code: (Select All)
To Be: date("Y") - date("Y"strtotime('Year-Month-Day'))
For 
Exampledate("Y") - date("Y"strtotime('2002-09-16'))) 
Output:
16

If you Wanna Calculate your real age with full details (Y/M/H/m/s/ms/ns/ms):
1- We initializate the $datebirth var with the DateTime Class and set the timestamp argument of the DateTime Class to your Date of Birth with the following Syntax Year-Month-Day
PHP Code: (Select All)
To Be$datebirth = new DateTime('Year-Month-Day');
For 
Example$datebirth = new DateTime('2002-09-16'); 
2- We initializate the $today var with the DateTime Class without setting any arguments to get Today's Timestamp
PHP Code: (Select All)
To Be$today = new DateTime
3- We do the algorithm to calculate the requested Person's Age with DateTime::diff and then to get the result we must use DateInterval::Format
PHP Code: (Select All)
For Example: echo $today->diff($dabi)->format('%y Years, %m Months, %d Days, %h Hours, %I Minutes, %s Seconds, ' . ($today->diff($dabi)->format('%s')*1000) . ' Milliseconds, ' . ($today->diff($dabi)->format('%s')*1000*1000000) . ' Nanoseconds, ' . ($today->diff($dabi)->format('%s')*1000*1000000*1000) . ' Microseconds'); 

Let's Discuss somethings in our small algorithm...
  • We multiply the seconds in one thousand to get the Milliseconds.(Second = 1000 Milliseconds)
  • We multiply the milliseconds in one thousand ms and the result get multiplied in one million ms to get the Nanosecond.(Nanosecond = 1000000 Millisecond)
  • We multiply the Microseconds in one thousand ms and the result get multiplied in one million ms and the second result get multiplied in one thousand, to get the Microsecond.(Microsecond = 1000 Nanosecond)

Output:
15 Years, 8 Months, 3 Days, 19 Hours, 25 Minutes, 45 Seconds, 45000 Milliseconds, 45000000000 Nanoseconds, 45000000000000 Microseconds

Download Full Example Code: https://gist.github.com/JohnMagdy/8f8411...31cb9ec602
Thanks to @Post4VPS & @Hostdare for Providing their VPS Hosting Services: VPS 12
#2
Nice one zorono..
i remember old times , when i used Pascal :p

Thanks to @Post4VPS & @Racknerd for Providing thier : VPS 2

#3
Simply use android app " age calculator.apk " for exact years, months, days, hours, etc ... also calculate difference between two ages
Heart LOVE FOR ALL  HATRED FOR NONE Heart
#4
Wow awesome! Keep it up bro Heart
Any tutorials for SAMP UCP & ACP?
#5
i don't need this script or any app to calculate my age Big Grin because i became 20th this 9th May.
Terminal
Best Regards,
Global Moderator of Post4VPS 

#6
Can it be accurate to seconds without we giving the time in second? I do not think it can be unless we input time in seconds, between a simple code.


Thank you  Sweet



#7
[attachment=90 Wrote:Littlemaster pid='19403' dateline='1527269993']Can it be accurate to seconds without we giving the time in second? I do not think it can be unless we input time in seconds, between a simple code.
   
http://php.net/manual/en/dateinterval.format.php

you might do it by using the following:
PHP Code: (Select All)
echo $today->diff($dabi)->format('%y Years, %m Months, %d Days, %h Hours, %I Minutes, '  . ($today->diff($dabi)->format('%I')*60) . 'Seconds, ' . ($today->diff($dabi)->format('%s')*1000) . ' Milliseconds, ' . ($today->diff($dabi)->format('%s')*1000*1000000) . ' Nanoseconds, ' . ($today->diff($dabi)->format('%s')*1000*1000000*1000) . ' Microseconds'); 
you can get the seconds by multiplying the Minutes in 60.
Thanks to @Post4VPS & @Hostdare for Providing their VPS Hosting Services: VPS 12
#8
(05-25-2018, 06:29 AM)arsalahmed786 Wrote: i don't need this script or any app to calculate my age Big Grin because i became 20th this 9th May.

Ohhhh... it's mean the sweet teen is gone. now you are going to mature sector of age.
Heart LOVE FOR ALL  HATRED FOR NONE Heart


Possibly Related Threads…
Thread
Author
Replies
Views
Last Post

person_pin_circle Users browsing this thread: 2 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting