is it get any wrong in this function? 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;} } ?>
i mean that, from one year - become one month and days. for example - one year get 12 month right? so - one year get 12 month and 365 days. and there is function for array to, get round the days and the month for a year. so, is it this function is correct or not? because whenever i calculate for one year must get 3, or two days extra.
I wouldn't use exact days etc.. I'd use the total "seconds" As the number of seconds a year are constant. Then divide it by 12 for a month.
do you mean something like this? i am not really sure, but this is some other part in the system. function datediff($interval, $datefrom, $dateto, $using_timestamps = false) { if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); $dateto = strtotime($dateto, 0); } $difference = $dateto - $datefrom; // Difference in seconds switch($interval) { case 'yyyy': // Number of full years $years_difference = floor($difference / 31536000); if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) { $years_difference--; } if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) { $years_difference++; } $datediff = $years_difference; break; case "q": // Number of full quarters $quarters_difference = floor($difference / 8035200); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $quarters_difference--; $datediff = $quarters_difference; break; case "m": // Number of full months $months_difference = floor($difference / 2678400); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $months_difference--; $datediff = $months_difference; break; case 'y': // Difference between day numbers $datediff = date("z", $dateto) - date("z", $datefrom); break; case "d": // Number of full days $datediff = floor($difference / 86400); break; case "w": // Number of full weekdays $days_difference = floor($difference / 86400); $weeks_difference = floor($days_difference / 7); // Complete weeks $first_day = date("w", $datefrom); $days_remainder = floor($days_difference % 7); $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder? if ($odd_days > 7) { // Sunday $days_remainder--; } if ($odd_days > 6) { // Saturday $days_remainder--; } $datediff = ($weeks_difference * 5) + $days_remainder; break; case "ww": // Number of full weeks $datediff = floor($difference / 604800); break; case "h": // Number of full hours $datediff = floor($difference / 3600); break; case "n": // Number of full minutes $datediff = floor($difference / 60); break; default: // Number of full seconds (default) $datediff = $difference; break; } return $datediff; }
maybe. actually this is from a system. the problem is the service length of the system. there got an extra days when i calculate it. so, i thought, maybe the problem is coming from that function. i am not really sure yet.
As I didn't know exactly what information you wanted out ... I've guessed that you want Years, Months & Days. function decimalYear($dec){ $r = Array( Array("Years" => 0, "Months" => 0, "Days" => 0), Array("Years" => 31556926, "Months" => 2629743.83, "Days" => 86400) ); $dec = $dec * 31556926; foreach($r[1] as $key => $item){ while($dec >= $item){ $dec = $dec - $item; $r[0][$key]++; } } return $r[0]; } PHP: That will retun an array in the structure: Array ( [Years] => 0 [Months] => 0 [Days] => 0 ) Code (markup): Simply call it through: decimalYear(3.5); PHP:
thank but i am not really sure to use your function. there is the function that have in the system. the problem is whenever i calculate the service length, there have an extra. so, i just want to know which part is wrong? so i can correct it. if i use your function, i have to reconstruct all of the system, because every leave have the leave summary. //LEAVE SUMMARY************************************************************************************************************************************************************** function dateDiff($dformat, $endDate, $beginDate) { $date_parts1=explode($dformat, $beginDate); $date_parts2=explode($dformat, $endDate); $start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]); return $end_date - $start_date + 1; } $query200 = "SELECT * FROM leave_selection WHERE '$apply_year-01-01' BETWEEN dari AND hingga"; $result200 = mysql_query($query200) or die("couldn't execute query"); $row200 = mysql_fetch_array($result200); $c_date = date('d-m-Y',strtotime($row1['commencement_date'])); $service_year = round(dateDiff("-", date("31-12-Y",time()), $c_date)/365,2); $zarbi=explode('-',$c_date); $Day = $zarbi[0]; $Month = $zarbi[1]; $Year = $zarbi[2]; $query201= "SELECT * FROM annual_leave WHERE ('$service_year' BETWEEN service_length AND service_length1) && ('$Day' BETWEEN joining_date AND joining_date1) "; $result201 = mysql_query($query201) or die("2couldn't execute query"); $row201 = mysql_fetch_array($result201); //to get leave entitlement(days) based on service period && day and month of commencement date if ($Month == '01'){$annual_ed = $row201['january'];} elseif ($Month == '02'){$annual_ed = $row201['february'];} elseif ($Month == '03'){$annual_ed = $row201['march'];} elseif ($Month == '04'){$annual_ed = $row201['april'];} elseif ($Month == '05'){$annual_ed = $row201['may'];} elseif ($Month == '06'){$annual_ed = $row201['jun'];} elseif ($Month == '07'){$annual_ed = $row201['july'];} elseif ($Month == '08'){$annual_ed = $row201['august'];} elseif ($Month == '09'){$annual_ed = $row201['september'];} elseif ($Month == '10'){$annual_ed = $row201['october'];} elseif ($Month == '11'){$annual_ed = $row201['november'];} elseif ($Month == '12'){$annual_ed = $row201['december'];} $query202= "SELECT SUM(carryforward) FROM leave_application Where ((status='6') || ( (name_tm LIKE '%') &&(status='15' || status='16' || status='14' || status='12'))) && NRIC = '".$_SESSION[NRIC]."' && NRIC1 = '".$_SESSION[NRIC1]."' && NRIC2 = '".$_SESSION[NRIC2]."' && leave_code = '".$row2['lc']."' && dari LIKE '$apply_year%' "; $result202= mysql_query($query202) or die("as execute query"); $row202 = mysql_fetch_array($result202); $annual_balance = $annual_ed-$row120['SUM(total)']+$row202['SUM(carryforward)']; $query203 = "SELECT * FROM prorated WHERE leave_entitlement = '$annual_ed'"; $result203 = mysql_query($query203) or die("4couldn't execute query"); $row203 = mysql_fetch_array($result203); $apply_month = date('m'); //for QUARTERLY, to get currently available leave entitlement(days) based on quarter apply if ($apply_month == '01'){$quarter_curr = $row300['quarter1']; $current_quarter = "JAN , FEB , MARCH";} elseif ($apply_month == '02'){$quarter_curr = $row203['quarter1']; $current_quarter = "JAN , FEB , MARCH";} elseif ($apply_month == '03'){$quarter_curr = $row203['quarter1']; $current_quarter = "JAN , FEB , MARCH";} elseif ($apply_month == '04'){$quarter_curr = $row203['quarter2']; $current_quarter = "APR , MAY , JUNE";} elseif ($apply_month == '05'){$quarter_curr = $row203['quarter2']; $current_quarter = "APR , MAY , JUNE";} elseif ($apply_month == '06'){$quarter_curr = $row203['quarter2']; $current_quarter = "APR , MAY , JUNE";} elseif ($apply_month == '07'){$quarter_curr = $row203['quarter3']; $current_quarter = "JUL , AUG , SEPT";} elseif ($apply_month == '08'){$quarter_curr = $row203['quarter3']; $current_quarter = "JUL , AUG , SEPT";} elseif ($apply_month == '09'){$quarter_curr = $row203['quarter3']; $current_quarter = "JUL , AUG , SEPT";} elseif ($apply_month == '10'){$quarter_curr = $row203['quarter4']; $current_quarter = "OCT , NOV , DEC";} elseif ($apply_month == '11'){$quarter_curr = $row203['quarter4']; $current_quarter = "OCT , NOV , DEC";} elseif ($apply_month == '12'){$quarter_curr = $row203['quarter4']; $current_quarter = "OCT , NOV , DEC";} $previous_year = $apply_year-1; $query204 = "SELECT * FROM leave_selection WHERE '$previous_year-01-01' BETWEEN dari AND hingga"; $result204 = mysql_query($query204) or die("couldn't execute query"); $row204 = mysql_fetch_array($result204); $previous_service_year = round(dateDiff("-", date("31-12-$previous_year",time()), $c_date)/365,2);