Follow Me

How to calculate days remaining in date of birth

Sat, May 22, 2010

PHP

How to calculate days remaining in date of birth from current time? One day I need to find how many days remain in someone’s date of birth. Therefore I wrote this small script for calculating how many days remain in date of birth. It will display no. of days, for this I used dateDiff function. It simply gives diff between two dates.

Before dateDiff, I checked that current time and birthday time because if current time is more than birthday time then we have to add one more year in the birthday date so that It will calculate diff of days from next year birthday.

<?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]);
	if($end_date - $start_date<0)
	return -($end_date - $start_date);
	else
	return $end_date - $start_date;
}

// set fixed date and month year of birth
$dob="07/14/".date('Y');

// get current date month year
$cdate=date("m/d/Y", time());

$a=strtotime($dob);
$b=strtotime($cdate);

// calculate diff if dateofbirth time is less then add one more year to it
if($a<$b)
{
	$dob1=explode("/",$dob);
	$dob_new=$dob1[2]+1;
	$dob2=implode("/",array($dob1[0],$dob1[1],$dob_new));
	$dob=$dob2;
}

// finally we get remaining days
echo "Birthday :  ".dateDiff("/", $cdate, $dob). " days away.";
?>

Enjoy it :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • Leave a Reply