Post4VPS Forum | Free VPS Provider
How to Calculate your Age with full detailes by Date of Birth (PHP) - Printable Version

+- Post4VPS Forum | Free VPS Provider (https://post4vps.com)
+-- Forum: VPS Discussion (https://post4vps.com/Forum-VPS-Discussion)
+--- Forum: Tutorials (https://post4vps.com/Forum-Tutorials)
+--- Thread: How to Calculate your Age with full detailes by Date of Birth (PHP) (/Thread-How-to-Calculate-your-Age-with-full-detailes-by-Date-of-Birth-PHP)



How to Calculate your Age with full detailes by Date of Birth (PHP) - Zorono - 05-19-2018

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:
To Be: date("Y") - date("Y"strtotime('Year-Month-Day'))
For 
Exampledate("Y") - date("Y"strtotime('2002-09-16'))) 
Output:
Code:
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:
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:
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:
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:
Code:
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/8f8411d70db75480ebfd9031cb9ec602


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - YOuNeS_Dz - 05-20-2018

Nice one zorono..
i remember old times , when i used Pascal :p


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - sagher - 05-21-2018

Simply use android app " age calculator.apk " for exact years, months, days, hours, etc ... also calculate difference between two ages


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - youssefbasha - 05-24-2018

Wow awesome! Keep it up bro Heart
Any tutorials for SAMP UCP & ACP?


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - arsalahmed786 - 05-25-2018

i don't need this script or any app to calculate my age Big Grin because i became 20th this 9th May.


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - Littlemaster - 05-25-2018

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.


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - Zorono - 05-25-2018

[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:
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.


RE: How to Calculate your Age with full detailes by Date of Birth (PHP) - sagher - 05-26-2018

(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.