calculating date differences

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 5, 2011   (RSS)

By Deborah - June 3, 2011

I am setting up an employee portal where we want to display the hire date AND how much time has passed for each hire date to current date of when the page is being viewed.

Example results (based on a current date being 06/03/2011):

$hire_date = 02/01/2000
result = 11 years, 3 months

$hire_date = 05/02/2011
result = 1 month, 1 day

$hire_date = 06/01/2011
result = 2 days

The calculations would have to take into account leap years, but need not be precise to the second.

Does anyone have a formula for arriving at the results shown?

Thanks in advance for any help.

~ Deborah

Re: [gkornbluth] calculating date differences

By Deborah - June 5, 2011

Jerry,

Thank you for the date comparison recipes. I find those examples from the Cookbook very useful and have saved them for future use.

Meanwhile, I was able to expand upon an earlier post by Dave to arrive at a solution:
http://www.interactivetools.com/forum/gforum.cgi?post=72534;search_string=how%20many%20days;t=search_engine#72534

I decided to display just the number of years or months (if less than one year) since the hire date. The following code seems to be working:

<?php
$today = time() - strtotime($accountsRecord['hire_date']);
$yearsOld = intval($today/60/60/24/7/4.35/12);
$monthsOld = intval($today/60/60/24/7/4.35); ?>

<?php if ($yearsOld < 1):{echo $monthsOld;} ?> months
<?php elseif ($yearsOld == 1):{echo "1 year";} ?>
<?php else: ?><?php echo $yearsOld ?> years
<?php endif ?>

Not sure if this will account for leap year date hires, but I don't need to be that precise for this application.

Thanks again.
~ Deborah

Re: [Deborah] calculating date differences

By gkornbluth - June 5, 2011

Glad it's working, and I'll file this one away as well.

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php