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;}
}``````

Edited by nav33n: Wrap your code in [code][/code] tags for easy readability.

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by daryll1

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

``````<?php
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));
//seconds
if (\$d1['second'] >= \$d2['second']){
\$diff['second'] = \$d1['second'] - \$d2['second'];
}
else {
\$d1['minute']--;
\$diff['second'] = 60-\$d2['second']+\$d1['second'];
}
//minutes
if (\$d1['minute'] >= \$d2['minute']){
\$diff['minute'] = \$d1['minute'] - \$d2['minute'];
}
else {
\$d1['hour']--;
\$diff['minute'] = 60-\$d2['minute']+\$d1['minute'];
}
//hours
if (\$d1['hour'] >= \$d2['hour']){
\$diff['hour'] = \$d1['hour'] - \$d2['hour'];
}
else {
\$d1['day']--;
\$diff['hour'] = 24-\$d2['hour']+\$d1['hour'];
}
//days
if (\$d1['day'] >= \$d2['day']){
\$diff['day'] = \$d1['day'] - \$d2['day'];
}
else {
\$d1['month']--;
\$diff['day'] = date("t",\$temp)-\$d2['day']+\$d1['day'];
}
//months
if (\$d1['month'] >= \$d2['month']){
\$diff['month'] = \$d1['month'] - \$d2['month'];
}
else {
\$d1['year']--;
\$diff['month'] = 12-\$d2['month']+\$d1['month'];
}
//years
\$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())));
print_r(\$date_diff_array);
?>``````

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.

</html>

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.