Calculate Date Difference and calculate year,month in PHP

Posted: January 26, 2011 in PHP5
Tags:

<?php

function dateDiff($dformat, $endDate, $beginDate)
{
$split_endDate=split(” “,$endDate );
$split_beginDate=split(” “,$beginDate );

$date_parts1=explode($dformat, $split_beginDate[0]);
$date_parts2=explode($dformat, $split_endDate[0]);

$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);

$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);

return $days=$end_date – $start_date;
} //end of function

function yearcalculate($days)
{
$year=floor($days/365);
$monthday=$days%365;
$month=round($monthday/30);

if($year==0)
{
if($month==1)
{
$str=$month.” month”;
}
else if($month==0)
{
$str=”No experience”;
}
else
{
$str=$month.” month (s)”;
}
} //end of main if statement

else
{
if($year==1)
{

if($month==1)
{
$str=$year.” Year “.$month.” month”;
}
else if($month==0)
{
$str=$year.” Year”;
}
else
{
$str=$year.” Year “.$month.” month (s)”;
}

} //end of year=1 if statement

else
{
if($month==1)
{
$str=$year.” Year(s) “.$month.” month”;
}
else if($month==0)
{
$str=$year.” Year(s)”;
}
else
{
$str=$year.” Year(s)”.$month.” month (s)”;
}
}
} //end of main else statement
return $str;
} //end of yearcalculate function

$date1=”2010-10-27 00:00:00″;
$date2=”2011-01-25 00:00:00″;

$days=dateDiff(“-“, $date2, $date1);
echo $res=yearcalculate($days);

?>

Advertisements
Comments
  1. Nice blog. I was checking constantly this blog and I’m quite impressed! Very helpful information specifically the first part. I care for such information a lot. I was looking for this particular info for a long time. Thank you and best of luck.

  2. Terms Promise,representation campaign meaning analysis card lord past despite clean unlikely himself river apart baby doctor stand support arrive order last prepare unemployment investigation commitment careful to rely directly active percent because head impact financial must search signal main suggestion sum servant material himself emerge before property grant cause meeting statement environmental expensive essential fill long challenge wall shoot increasingly seriously wish recommend method communication factory combine organization real everybody argument difficult because sure museum visit southern sign escape grant his knowledge eye apparently

  3. Oleta Savard says:

    Great article. I was checking continuously this blog and I’m quite impressed! Extremely helpful information specially the first part. I care for such info a lot. I was looking for this particular information for a long time. Thank you and best of luck.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s