///Calculating Difference Between Two Dates Using PHP

Calculating Difference Between Two Dates Using PHP

Calculating difference between two dates using PHP

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:

Sheila is 29 years old
Fatima is 33 years old
Peter is 17 years old
… and so on

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. First the function (I’ll discuss it later on)

function dateDiff($dformat, $endDate, $beginDate)
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date – $start_date;

Now let us see how we use this function:


print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";

which generates If we minus 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[0] holds "Trees", $stringparts[1] holds "plants" and $stringparts[2] holds "bushes". The separator is ", ". Similarly, in the above example, $date_parts1[0] holds the month part, $date_parts1[1] holds the day part and $date_parts1[2] 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.

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.

2010-05-25T23:04:18+00:00 October 18th, 2005|PHP|0 Comments

About the Author:

Amrit Hallan is a freelance web developer. You can checkout his website at http://www.bytesworth.com. For more such articles join BYTESWORTH REACHOUT at http://www.bytesworth.com/br/default.asp or if you have a web dev related question then post it at http://www.business180.com/index.php

Leave A Comment