How to calculate your age
Php,Java,python,ruby ways.
PHP Ways
Function getMyAge($bithdayDate)
{
$date = new DateTime($bithdayDate);
$now = new DateTime();
$interval = $now->diff($date);
return $interval->y;
}
Python ways
from datetime import date
def calculate_age(born):
today = date.today()
try:
birthday = born.replace(year=today.year)
except ValueError: # raised when birth date is February 29 and the current year is not a leap year
birthday = born.replace(year=today.year, month=born.month+1, day=1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
Ruby & Rails Way
• from datetime import date
• def calculate_age(born):
• today = date.today()
• try:
• birthday = born.replace(year=today.year)
• except ValueError: # raised when birth date is February 29 and the current year is not a leap
year
• birthday = born.replace(year=today.year, month=born.month+1, day=1)
• if birthday > today:
• return today.year - born.year - 1
• else:
• return today.year - born.year
Java ways
LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
Live Website Using those algorithm
http://www.todaymyage.com

How to calculate your age

  • 1.
    How to calculateyour age Php,Java,python,ruby ways.
  • 2.
    PHP Ways Function getMyAge($bithdayDate) { $date= new DateTime($bithdayDate); $now = new DateTime(); $interval = $now->diff($date); return $interval->y; }
  • 3.
    Python ways from datetimeimport date def calculate_age(born): today = date.today() try: birthday = born.replace(year=today.year) except ValueError: # raised when birth date is February 29 and the current year is not a leap year birthday = born.replace(year=today.year, month=born.month+1, day=1) if birthday > today: return today.year - born.year - 1 else: return today.year - born.year
  • 4.
    Ruby & RailsWay • from datetime import date • def calculate_age(born): • today = date.today() • try: • birthday = born.replace(year=today.year) • except ValueError: # raised when birth date is February 29 and the current year is not a leap year • birthday = born.replace(year=today.year, month=born.month+1, day=1) • if birthday > today: • return today.year - born.year - 1 • else: • return today.year - born.year
  • 5.
    Java ways LocalDate birthdate= new LocalDate (1970, 1, 20); LocalDate now = new LocalDate(); Years age = Years.yearsBetween(birthdate, now);
  • 6.
    Live Website Usingthose algorithm http://www.todaymyage.com