i pokazuję ją tak:
$rr = mysql_fetch_array(ZAPYTANIE);
$wiek = $rr['data_ur'] <>'0'? number_format(date_diff($rr['data_ur'],date('d-m-Y'))/365,0): '??';
i moja funckja date_diff wygląda następująco:
<?php function date_diff($date_from,$date_to) /* Calculates difference in days from date_from to date_to, taking into account lea
p years if date_from > date_to, the number of days is returned negative date_from and date_to format is: "dd-mm-yyyy" It can calculate ANY date difference, for example between 21-4-345 and 11-11-3412 This is possible by mapping any date to the "range 0" dates, as this table shows: INI END RANGE LEAP YEARS ... ... ... ... 01/01/1920 01/01/1939 -3 5 01/01/1940 01/01/1959 -2 5 01/01/1960 01/01/1979 -1 5 01/01/1980 01/01/1999 0 5 * this is the range used for calculations with mktime 01/01/2000 01/01/2019 1 5 01/01/2020 01/01/2039 2 5 01/01/2040 01/01/2059 3 5 01/01/2060 01/01/2079 4 5 ... ... ... ... */ { $day_from = $date_from_parts[0]; $mon_from = $date_from_parts[1]; $year_from = $date_from_parts[2]; $day_to = $date_to_parts[0]; $mon_to = $date_to_parts[1]; $year_to = $date_to_parts[2]; //if date_from is newer than date to, invert dates $sign=1; if ($year_from>$year_to) $sign=-1; else if ($year_from==$year_to) { if ($mon_from>$mon_to) $sign=-1; else if ($mon_from==$mon_to) if ($day_from>$day_to) $sign=-1; } if ($sign==-1) {//invert dates $day_from = $date_to_parts[0]; $mon_from = $date_to_parts[1]; $year_from = $date_to_parts[2]; $day_to = $date_from_parts[0]; $mon_to = $date_from_parts[1]; $year_to = $date_from_parts[2]; } $yearfrom1=$year_from; //actual years $yearto1=$year_to; //(yearfrom2 and yearto2 are used to calculate inside the range "0") //checks ini date if ($yearfrom1<1980) {//year is under range 0 $yearfrom2=$yearfrom1+$deltafrom; //year used for calculations } else if($yearfrom1>1999) {//year is over range 0 $yearfrom2=$yearfrom1-$deltafrom; //year used for calculations } else {//year is in range 0 $deltafrom=0; $yearfrom2=$yearfrom1; } //checks end date if ($yearto1<1980) {//year is under range 0 $yearto2=$yearto1+$deltato; //year used for calculations } else if($yearto1>1999) {//year is over range 0 $yearto2=$yearto1-$deltato; //year used for calculations } else {//year is in range 0 $deltato=0; $yearto2=$yearto1; } //Calculates the UNIX Timestamp for both dates (inside range 0) $diff = ($ts_to-$ts_from)/86400; //adjust ranges $diff += 7305 * (($deltafrom+$deltato) / 20); return $sign*$diff; } ?>
i problem pojawia się w tedy jeżeli user poda datę, a dokładnie jeżeli urodził się danego dnia:
06-03-2000
powinno pokazać że ma 7 lat a tym czasem niestety nie.. pokazuje że ma 47 LAT
i ten błąd występuje tylko jeżeli rok urodzenia wynosi 2000 .....