i am creating a system and have problem with the function to convert year - year, month and date.
the thing that i understand is the year is round for 365 days, and others i am not really understand of all. can you give explanation for this function so that i can correct it? thank you.

function year2monthsNdays($years)
$array    = explode(".",$years);
$year    = $array[0];
$month    = ($array[1]>9) ? $array[1]/100 : $array[1]/10;
if ($month) {
$days        = round($month*365,2);
$daysArray    = explode(".",$days);
$months        = round($daysArray[0]/30,2);
$monthArray    = explode(".",$months);
$monthInt    = $monthArray[0];
$daysInt    = round($monthArray[1]*30/100,1);
$a    = "$year-$monthInt-$daysInt";

return $a;

$service_year_convert = year2monthsNdays($service_year); 

$convert =explode('-',$service_year_convert);
$sl_year = $convert[0];   if($sl_year == NULL){$s_year = 0;} else{$s_year = $sl_year;}
$sl_month = $convert[1];  if($sl_month == NULL){$s_month = 0;} else{$s_month = $sl_month;}
$sl_day = round($convert[2]); 	if($sl_day == NULL){$s_day = 0;} else{$s_day = $sl_day;}

Can you please provide an example on what your input will be and the output expected from the function ?

for example. a staff, enter a company, and the first date he work is
1 July 2008. so, today date is 24 March 2010. Supposely, when calculate the service length (how many years, month, and days) he works here supposely be 1 year, 8 month, and 24 days. but the result that i got is 1 years, 8 month and 26 days. there an extra days about two days. so, maybe this function got wrong. so, hope you can help.

Hi, i found the following code snippet in php.net site which gives you the exact difference between 2 dates ( even with the precision of seconds if you want to ). I have made some small changes to remove the precision of hours, minutes and seconds. This is rather simple to understand. Please go through the link below.


Also the code snippet which will be useful for you has been posted by davix
06-Oct-2009 07:39

Following is the code snippet will small change in the input provided to suit your needs

function date_diff($d1, $d2){
/* compares two timestamps and returns array with differencies (year, month, day, hour, minute, second)
  //check higher timestamp and switch if neccessary
  if ($d1 < $d2){
    $temp = $d2;
    $d2 = $d1;
    $d1 = $temp;
  else {
    $temp = $d1; //temp can be used for day count if required
  $d1 = date_parse(date("Y-m-d H:i:s",$d1));
  $d2 = date_parse(date("Y-m-d H:i:s",$d2));
  if ($d1['second'] >= $d2['second']){
    $diff['second'] = $d1['second'] - $d2['second'];
  else {
    $diff['second'] = 60-$d2['second']+$d1['second'];
  if ($d1['minute'] >= $d2['minute']){
    $diff['minute'] = $d1['minute'] - $d2['minute'];
  else {
    $diff['minute'] = 60-$d2['minute']+$d1['minute'];
  if ($d1['hour'] >= $d2['hour']){
    $diff['hour'] = $d1['hour'] - $d2['hour'];
  else {
    $diff['hour'] = 24-$d2['hour']+$d1['hour'];
  if ($d1['day'] >= $d2['day']){
    $diff['day'] = $d1['day'] - $d2['day'];
  else {
    $diff['day'] = date("t",$temp)-$d2['day']+$d1['day'];
  if ($d1['month'] >= $d2['month']){
    $diff['month'] = $d1['month'] - $d2['month'];
  else {
    $diff['month'] = 12-$d2['month']+$d1['month'];
  $diff['year'] = $d1['year'] - $d2['year'];
  return $diff;   

$born_date = mktime(0,0,0,7,1,2008);
$date_diff_array = date_diff($born_date, strtotime(date("Y-m-d",time())));

I hope it helps.

get error

Parse error: syntax error, unexpected $end in C:\Program Files\xampp\htdocs\eleavechj\annual_leave_application_form.php on line 1965

That error may be because of missing curly bracket. I can't help without seeing the code.


this is the coding for line 1965

The curly brace won't be on line 1965 that's just where PHP is getting the error as it expects something else to come.
Try using a program that pairs up the braces like notepad++, Kate or Bluefish.
Or even just spend a bit of time going through the code and see if something is missing, I usually find its so obvious that I overlook it the first time.