Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Obliczanie wieku
Forum PHP.pl > Forum > Przedszkole
fredzio90
otóż mam malutki problemik... otóż mam bazę, w której jest pole do ktorego zpaisuje date urodzenia zarejtsrowanego usera..... i jest ona w tkaiej postaci: DD-MM-RRRR
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:

  1. <?php
  2. function date_diff($date_from,$date_to)
  3. /*
  4. Calculates difference in days from date_from to date_to, taking into account lea
    p years
  5. if date_from > date_to, the number of days is returned negative
  6. date_from and date_to format is: "dd-mm-yyyy"
  7. It can calculate ANY date difference, for example between 21-4-345 and 11-11-3412
  8. This is possible by mapping any date to the "range 0" dates, as this table shows:
  9.  
  10. INI END RANGE LEAP YEARS
  11. ... ... ... ...
  12. 01/01/1920 01/01/1939 -3 5
  13. 01/01/1940 01/01/1959 -2 5
  14. 01/01/1960 01/01/1979 -1 5
  15. 01/01/1980 01/01/1999 0 5 * this is the range used for calculations with mktime
  16. 01/01/2000 01/01/2019 1 5
  17. 01/01/2020 01/01/2039 2 5
  18. 01/01/2040 01/01/2059 3 5
  19. 01/01/2060 01/01/2079 4 5
  20. ... ... ... ...
  21. */
  22.  
  23. {
  24. $date_from_parts = explode('-', $date_from);
  25. $date_to_parts = explode('-', $date_to);
  26. $day_from = $date_from_parts[0];
  27. $mon_from = $date_from_parts[1];
  28. $year_from = $date_from_parts[2];
  29. $day_to = $date_to_parts[0];
  30. $mon_to = $date_to_parts[1];
  31. $year_to = $date_to_parts[2];
  32.  
  33. //if date_from is newer than date to, invert dates
  34. $sign=1;
  35. if ($year_from>$year_to) $sign=-1;
  36. else if ($year_from==$year_to)
  37. {
  38. if ($mon_from>$mon_to) $sign=-1;
  39. else if ($mon_from==$mon_to)
  40. if ($day_from>$day_to) $sign=-1;
  41. }
  42.  
  43. if ($sign==-1) {//invert dates
  44. $day_from = $date_to_parts[0];
  45. $mon_from = $date_to_parts[1];
  46. $year_from = $date_to_parts[2];
  47. $day_to = $date_from_parts[0];
  48. $mon_to = $date_from_parts[1];
  49. $year_to = $date_from_parts[2];
  50. }
  51.  
  52. $yearfrom1=$year_from; //actual years
  53. $yearto1=$year_to; //(yearfrom2 and yearto2 are used to calculate inside the range "0")
  54.  
  55. //checks ini date
  56. if ($yearfrom1<1980)
  57. {//year is under range 0
  58. $deltafrom=floor((1999-$yearfrom1)/20)*20; //delta t1
  59. $yearfrom2=$yearfrom1+$deltafrom; //year used for calculations
  60. }
  61. else if($yearfrom1>1999)
  62. {//year is over range 0
  63. $deltafrom=floor(($yearfrom1-1980)/20)*20; //delta t1
  64. $yearfrom2=$yearfrom1-$deltafrom; //year used for calculations
  65. }
  66. else
  67. {//year is in range 0
  68. $deltafrom=0;
  69. $yearfrom2=$yearfrom1;
  70. }
  71.  
  72. //checks end date
  73. if ($yearto1<1980)
  74. {//year is under range 0
  75. $deltato=floor((1999-$yearto1)/20)*20; //delta t2
  76. $yearto2=$yearto1+$deltato; //year used for calculations
  77. }
  78. else if($yearto1>1999)
  79. {//year is over range 0
  80. $deltato=floor(($yearto1-1980)/20)*20; //delta t2
  81. $yearto2=$yearto1-$deltato; //year used for calculations
  82. }
  83. else
  84. {//year is in range 0
  85. $deltato=0;
  86. $yearto2=$yearto1;
  87. }
  88.  
  89. //Calculates the UNIX Timestamp for both dates (inside range 0)
  90. $ts_from = mktime(0, 0, 0, $mon_from, $day_from, $yearfrom2);
  91. $ts_to = mktime(0, 0, 0, $mon_to, $day_to, $yearto2);
  92. $diff = ($ts_to-$ts_from)/86400;
  93. //adjust ranges
  94. $diff += 7305 * (($deltafrom+$deltato) / 20);
  95. return $sign*$diff;
  96. }
  97. ?>


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 .....
Daimos
moze ta funkcja zalatwi sprawe winksmiley.jpg
fredzio90
dziękuje serdecznie, działa doskonale tongue.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.