SlideShare a Scribd company logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<?php
// Part 1: Use date("xxx") with only one parameter to output TODAY's info
// such as current day, month and year.
$today_day = date('d');
$today_month = date('m');
$today_year = date('Y');
echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>';
// Today's date is 03/02/2014
// Part 2: My date of birth.
$dob_day = '31';
$dob_month = '01';
$dob_year = '1968';
// Output method 1: using . and strings:
echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>';
// My date of birth is 31/01/1968
// Output method 2: Using mktime( ) to create the timestamp:
$dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year);
echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>';
// My date of birth is 31/01/1968
// Part 3: Calculate age.
if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1)
{
$age = $today_year - $dob_year -1;
echo 'Path 1 <br>'; // Debugging message
}

Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 1/2
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

elseif ($dob_month == $today_month) // Just reach birthday month this year.
{
if ($dob_day > $today_day) // Not yet reach birthday day this year (need to minus 1)
{
$age = $today_year - $dob_year -1;
echo 'Path 2 <br>'; // Debugging message
}
else // Already reach birthday day this year.
{
$age = $today_year - $dob_year;
echo 'Path 3 <br>'; // Debugging message
} // End of else for day.
}
else // Already after birthday month this year.
{
$age = $today_year - $dob_year;
echo 'Path 4 <br>'; // Debugging message
}
echo 'I am now ' . $age . ' years old. <br>';
// Path 4
// I am now 46 years old.
?>

Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 2/2

More Related Content

PPT
The Happiest Jobs in US
PPSX
2010 - Irish's Birthday
PPT
Gloria Korian Birthday Celebration Presentation1
PPSX
Julianne Friend's Birthday Preparation Event Photo Album
PPTX
Birthday events apiを作ってみたとか
PPT
Ma niketan 26th sept'10
PDF
Happy Birthday Emma Watson
PPSX
Happy bday sid
The Happiest Jobs in US
2010 - Irish's Birthday
Gloria Korian Birthday Celebration Presentation1
Julianne Friend's Birthday Preparation Event Photo Album
Birthday events apiを作ってみたとか
Ma niketan 26th sept'10
Happy Birthday Emma Watson
Happy bday sid

Viewers also liked (10)

DOCX
Birthday
PPTX
Mady's 1st Birthday Slideshow
PDF
Creating Beautiful, Accessible, and User-Friendly Forms
PPT
New born screeing
PPT
Happy Birthday, My Love, 2009
PPSX
Bangladesh (বংলাদেশ)
PPTX
A birthday presentation.2
PDF
Awesome Surprise Birthday Gift Ideas 2013
PPSX
Favorite moment in my life
PPT
My Birthday! Powerpoint
Birthday
Mady's 1st Birthday Slideshow
Creating Beautiful, Accessible, and User-Friendly Forms
New born screeing
Happy Birthday, My Love, 2009
Bangladesh (বংলাদেশ)
A birthday presentation.2
Awesome Surprise Birthday Gift Ideas 2013
Favorite moment in my life
My Birthday! Powerpoint
Ad

Similar to PHP built-in functions date( ) and mktime( ) to calculate age from date of birth (8)

PDF
PHP built-in function mktime example
PPTX
How to calculate your age
PDF
A simple php exercise on date( ) function
PDF
The Art of Transduction
PPT
MySQL Built-In Functions
 
ODP
Google Cloud Challenge - PHP - DevFest GDG-Cairo
PPT
MySQL Functions
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PHP built-in function mktime example
How to calculate your age
A simple php exercise on date( ) function
The Art of Transduction
MySQL Built-In Functions
 
Google Cloud Challenge - PHP - DevFest GDG-Cairo
MySQL Functions
Quick beginner to Lower-Advanced guide/tutorial in PHP
Ad

More from Hock Leng PUAH (20)

PDF
ASP.net Image Slideshow
PDF
Visual basic asp.net programming introduction
PDF
Using iMac Built-in Screen Sharing
PDF
Hosting SWF Flash file
PDF
Integrate jQuery PHP MySQL project to JOOMLA web site
PPTX
Responsive design
PDF
Step by step guide to use mac lion to make hidden folders visible
PPTX
Beautiful web pages
PPT
CSS Basic and Common Errors
PPTX
Connectivity Test for EES Logic Probe Project
PPTX
Logic gate lab intro
PDF
Ohm's law, resistors in series or in parallel
PPTX
Connections Exercises Guide
PPTX
Design to circuit connection
PPTX
NMS Media Services Jobshet 1 to 5 Summary
DOCX
Virtualbox step by step guide
PPTX
Nms chapter 01
PPTX
Pedagogic Innovation to Engage Academically Weaker Students
PPTX
Objective C Primer (with ref to C#)
PPTX
Do While and While Loop
ASP.net Image Slideshow
Visual basic asp.net programming introduction
Using iMac Built-in Screen Sharing
Hosting SWF Flash file
Integrate jQuery PHP MySQL project to JOOMLA web site
Responsive design
Step by step guide to use mac lion to make hidden folders visible
Beautiful web pages
CSS Basic and Common Errors
Connectivity Test for EES Logic Probe Project
Logic gate lab intro
Ohm's law, resistors in series or in parallel
Connections Exercises Guide
Design to circuit connection
NMS Media Services Jobshet 1 to 5 Summary
Virtualbox step by step guide
Nms chapter 01
Pedagogic Innovation to Engage Academically Weaker Students
Objective C Primer (with ref to C#)
Do While and While Loop

Recently uploaded (20)

PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Pre independence Education in Inndia.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Business Ethics Teaching Materials for college
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
English Language Teaching from Post-.pdf
Cardiovascular Pharmacology for pharmacy students.pptx
Introduction and Scope of Bichemistry.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pre independence Education in Inndia.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Business Ethics Teaching Materials for college
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
O7-L3 Supply Chain Operations - ICLT Program
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
English Language Teaching from Post-.pdf

PHP built-in functions date( ) and mktime( ) to calculate age from date of birth

  • 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <?php // Part 1: Use date("xxx") with only one parameter to output TODAY's info // such as current day, month and year. $today_day = date('d'); $today_month = date('m'); $today_year = date('Y'); echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>'; // Today's date is 03/02/2014 // Part 2: My date of birth. $dob_day = '31'; $dob_month = '01'; $dob_year = '1968'; // Output method 1: using . and strings: echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>'; // My date of birth is 31/01/1968 // Output method 2: Using mktime( ) to create the timestamp: $dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year); echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>'; // My date of birth is 31/01/1968 // Part 3: Calculate age. if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1) { $age = $today_year - $dob_year -1; echo 'Path 1 <br>'; // Debugging message } Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 1/2
  • 2. 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 elseif ($dob_month == $today_month) // Just reach birthday month this year. { if ($dob_day > $today_day) // Not yet reach birthday day this year (need to minus 1) { $age = $today_year - $dob_year -1; echo 'Path 2 <br>'; // Debugging message } else // Already reach birthday day this year. { $age = $today_year - $dob_year; echo 'Path 3 <br>'; // Debugging message } // End of else for day. } else // Already after birthday month this year. { $age = $today_year - $dob_year; echo 'Path 4 <br>'; // Debugging message } echo 'I am now ' . $age . ' years old. <br>'; // Path 4 // I am now 46 years old. ?> Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 2/2