# Calculating difference between two dates using PHP

Discussion in 'PHP' started by pradeep, Sep 28, 2006.

Joined:
Apr 4, 2005
Messages:
1,645
87
Trophy Points:
0
Occupation:
Programmer
Location:
Kolkata, India

### Introduction

Suppose you want to know the age of a person given her birth date. Let’s say her birth date is "09-23-1969". Although in this case you might say that we don’t need a program to compute the age, but assuming there are 1000s of records in your database and for every person you have to print a report like:

Asha is 21 years old
Tanaz is 20 years old
Manindar is 24 years old
… and so on

### Background

In the database we don’t generally store what the person's age is — we store what the person's date of birth is. This is because given the date of birth, we can always deduct it from today's date and calculate the number of years that person has lived so far. Calculating date difference also comes handy if you are running a subscription-based website. You need to know for how many days a person has been using your website and when the subscription is due. So in this tip we learn to write a function that returns us the number of days elapsed between two dates.

### The function...

PHP:
``` function dateDiff(\$dformat, \$endDate, \$beginDate) {     \$date_parts1=explode(\$dformat, \$beginDate);     \$date_parts2=explode(\$dformat, \$endDate);     \$start_date=gregoriantojd(\$date_parts1, \$date_parts1, \$date_parts1);     \$end_date=gregoriantojd(\$date_parts2, \$date_parts2, \$date_parts2);     return \$end_date - \$start_date; } ```

### Using the code

Now let us see how we use this function:

PHP:
``` \$date1="07/11/2003"; \$date2="09/04/2004";   print "If we subtract " . \$date1 . " from " . \$date2 . " we get " . dateDiff("/", \$date2, \$date1) . "."; ```
which generates If we subtract 07/11/2003 from 09/04/2004 we get 421.

The dateDiff() functions uses two PHP functions viz., explode() and gregoriantojd(). The explode() function is used mostly to convert strings into arrays. It takes two arguments: the separator and the string. So if

\$string="Trees, plants, bushes";
\$stringparts=explode(", ", \$string);

then \$stringparts holds "Trees", \$stringparts holds "plants" and \$stringparts holds "bushes". The separator is ", ". Similarly, in the above example, \$date_parts1 holds the month part, \$date_parts1 holds the day part and \$date_parts1 holds the year part and these are the arguments that the function gregoriantojd() needs. The syntax of the gregoriantojd() function is

gregoriantojd(\$month, \$day, \$year)

But why do we need this function? Without going into the gory details, it changes the date into a big number from which another number (obtained from another date) can be deducted. Having said that, let us now see a few applications for this function: first the age thing. The following snippet of code gives us the age of a person according to the current date.

PHP:
``` \$dob="08/12/1975"; echo "If you were born on " . \$dob . ", then today your age is approximately " . round(dateDiff("/", date("m/d/Y", time()), \$dob)/365, 0) . " years."; ```
This generates a line appearing below:

If you were born on 08/12/1975, then today your age is approximately 30 years.