WebDev: Calculating difference between two dates using PHP
Dear Publisher,
You can publish this article wherever you feel like, as long as you use the resource box. Although it is not necessary to inform me, but I feel happy if some tell me that my article has been posted :-)
-- Amrit
==========================================
Article Title: Calculating difference between two dates using PHP
Category: WebDev
Copyright © 2005 Amrit Hallan
==========================================
== Article Begins ==
Note: you can see this article in a more interactive manner at
www.aboutwebdesigning.com/2005/10/05/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)
== Code begins ==
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;
}
== Code ends ==
Now let us see how we use this function:
== Code begins ==
$date1="07/11/2003";
$date2="09/04/2004";
print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";
== Code ends ==
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 an applications of this function: the age thing. The following snippet of code gives us the age of a person according to the current date (assuming the current date is "10/05/2005").
$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.
== Article Ends ==
About the author:
===============================================
Amrit Hallan is a freelance copywriter,
and a website content writer. He also dabbles
with PHP and HTML. For more tips and tricks in
PHP, JavaScripting, XML, CSS designing and
HTML, visit his blog at
www.aboutwebdesigning.com
===============================================
|