Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Calculating difference between two dates using PHP (http://www.go4expert.com/articles/calculating-difference-dates-using-php-t1483/)

pradeep 28Sep2006 11:49

Calculating difference between two dates using PHP
 

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



Code: PHP

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;
}

Using the code



Now let us see how we use this function:

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

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


All times are GMT +5.5. The time now is 07:24.