Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: 2 Funkcje dot. czasu
Forum PHP.pl > Forum > PHP
xamrex
Witam,
Używam na moim forum, modyfikacji jak urodziny.
Chciałbym teraz aby data tych urodzin była wyświetlana na mojej innej stornie WWW

Jako, że niektórzy urodzili się przed rokiem 1970, funkcja mktime() nie byłaby dobra;/

Podaję tutaj teraz 2 funkcje które generują datę:
  1. // Add function mkrealdate for Birthday MOD
  2. // the originate php "mktime()", does not work proberly on all OS, especially when going back in time
  3. // before year 1970 (year 0), this function "mkrealtime()", has a mutch larger valid date range,
  4. // from 1901 - 2099. it returns a "like" UNIX timestamp divided by 86400, so
  5. // calculation from the originate php date and mktime is easy.
  6. // mkrealdate, returns the number of day (with sign) from 1.1.1970.
  7.  
  8. function mkrealdate($day,$month,$birth_year)
  9. {
  10. // range check months
  11. if ($month<1 || $month>12) return "error";
  12. // range check days
  13. switch ($month)
  14. {
  15. case 1: if ($day>31) return "error";break;
  16. case 2: if ($day>29) return "error";
  17. $epoch=$epoch+31;break;
  18. case 3: if ($day>31) return "error";
  19. $epoch=$epoch+59;break;
  20. case 4: if ($day>30) return "error" ;
  21. $epoch=$epoch+90;break;
  22. case 5: if ($day>31) return "error";
  23. $epoch=$epoch+120;break;
  24. case 6: if ($day>30) return "error";
  25. $epoch=$epoch+151;break;
  26. case 7: if ($day>31) return "error";
  27. $epoch=$epoch+181;break;
  28. case 8: if ($day>31) return "error";
  29. $epoch=$epoch+212;break;
  30. case 9: if ($day>30) return "error";
  31. $epoch=$epoch+243;break;
  32. case 10: if ($day>31) return "error";
  33. $epoch=$epoch+273;break;
  34. case 11: if ($day>30) return "error";
  35. $epoch=$epoch+304;break;
  36. case 12: if ($day>31) return "error";
  37. $epoch=$epoch+334;break;
  38. }
  39. $epoch=$epoch+$day;
  40. $epoch_Y=sqrt(($birth_year-1970)*($birth_year-1970));
  41. $leapyear=round((($epoch_Y+2) / 4)-.5);
  42. if (($epoch_Y+2)%4==0)
  43. {// curent year is leapyear
  44. $leapyear--;
  45. if ($birth_year >1970 && $month>=3) $epoch=$epoch+1;
  46. if ($birth_year <1970 && $month<3) $epoch=$epoch-1;
  47. } else if ($month==2 && $day>28) return "error";//only 28 days in feb.
  48. //year
  49. if ($birth_year>1970)
  50. $epoch=$epoch+$epoch_Y*365-1+$leapyear;
  51. else
  52. $epoch=$epoch-$epoch_Y*365-1-$leapyear;
  53. return $epoch;
  54. }
  55.  
  56. // Add function realdate for Birthday MOD
  57. // the originate php "date()", does not work proberly on all OS, especially when going back in time
  58. // before year 1970 (year 0), this function "realdate()", has a mutch larger valid date range,
  59. // from 1901 - 2099.
  60. //it returns a "like" UNIX date format (only date, related letters may be used, due to the fact that
  61. // the given date value should already be divided by 86400 - leaving no time information left)
  62. // a input like a UNIX timestamp divided by 86400 is expected, so
  63. // calculation from the originate php date and mktime is easy.
  64. // e.g. realdate ("m d Y", 3) returns the string "1 3 1970"
  65.  
  66. // UNIX users should replace this function with the below code, since this should be faster
  67. //
  68. //function realdate($date_syntax="Ymd",$date=0)
  69. //{ return create_date($date_syntax,$date*86400+1,0); }
  70.  
  71. function realdate($date_syntax="Ymd",$date=0)
  72. {
  73. global $lang;
  74. $i=2;
  75. if ($date>=0)
  76. {
  77. return create_date($date_syntax,$date*86400+1,0);
  78. } else
  79. {
  80. $year= -(date%1461);
  81. $days = $date + $year*1461;
  82. while ($days<0)
  83. {
  84. $year--;
  85. $days+=365;
  86. if ($i++==3)
  87. {
  88. $i=0;
  89. $days++;
  90. }
  91. }
  92. }
  93. $leap_year = ($i==0) ? TRUE : FALSE;
  94. $months_array = ($i==0) ?
  95. array (0,31,60,91,121,152,182,213,244,274,305,335,366) :
  96. array (0,31,59,90,120,151,181,212,243,273,304,334,365);
  97. for ($month=1;$month<12;$month++)
  98. {
  99. if ($days<$months_array[$month]) break;
  100. }
  101.  
  102. $day=$days-$months_array[$month-1]+1;
  103. //you may gain speed performance by remove som of the below entry's if they are not needed/used
  104. return strtr ($date_syntax, array(
  105. 'a' => '',
  106. 'A' => '',
  107. '\\d' => 'd',
  108. 'd' => ($day>9) ? $day : '0'.$day,
  109. '\\D' => 'D',
  110. 'D' => $lang['day_short'][($date-3)%7],
  111. '\\F' => 'F',
  112. 'F' => $lang['month_long'][$month-1],
  113. 'g' => '',
  114. 'G' => '',
  115. 'H' => '',
  116. 'h' => '',
  117. 'i' => '',
  118. 'I' => '',
  119. '\\j' => 'j',
  120. 'j' => $day,
  121. '\\l' => 'l',
  122. 'l' => $lang['day_long'][($date-3)%7],
  123. '\\L' => 'L',
  124. 'L' => $leap_year,
  125. '\\m' => 'm',
  126. 'm' => ($month>9) ? $month : '0'.$month,
  127. '\\M' => 'M',
  128. 'M' => $lang['month_short'][$month-1],
  129. '\\n' => 'n',
  130. 'n' => $month,
  131. 'O' => '',
  132. 's' => '',
  133. 'S' => '',
  134. '\\t' => 't',
  135. 't' => $months_array[$month]-$months_array[$month-1],
  136. 'w' => '',
  137. '\\y' => 'y',
  138. 'y' => ($year>29) ? $year-30 : $year+70,
  139. '\\Y' => 'Y',
  140. 'Y' => $year+1970,
  141. '\\z' => 'z',
  142. 'z' => $days,
  143. '\\W' => '',
  144. 'W' => '') );
  145. }
  146. // End add - Birthday MOD



Możemy sobie wygenerować jakąś datę:
np
  1. <?php
  2. $birthday = mkrealdate(01,01,1990);
  3. echo $birthday;
  4. ?>


1 styczeń 1990 to taka data: 7305

Jak widać tworzy daty bardzo ładnie smile.gif

Mój problem to: Jak sprowadzić datę, która wyszła nam w funkcji na datę, którą człowiek potrafi odczytać.


Próbując analogicznie jak w przypadku 'kodowania' daty:
  1. $birthday = realdate('d M Y', 7306);
  2. echo $birthday;
  3. ?>


Otrzymujemy błąd :
  1. Call to undefined function: create_date()

Błąd ten wynika gdyż funkcja realdate ma coś takiego
Kod
return create_date
a tej funkcji nie mam;/



No, to poszukałem dalej na moim forum pliku, który ma tę funkcje.
oto ona:
  1. function create_date($format, $gmepoch, $tz)
  2. {
  3. global $board_config, $lang;
  4. static $translate;
  5.  
  6. if ( empty($translate) && $board_config['default_lang'] != 'english' )
  7. {
  8. @reset($lang['datetime']);
  9. while ( list($match, $replace) = @each($lang['datetime']) )
  10. {
  11. $translate[$match] = $replace;
  12. }
  13. }
  14.  
  15. return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  16. }


Niestety ta funkcja ma trochę zmiennych globalnych,
I Jeśli dodamy ją do naszego pliku, to skrypt również nie zadziała.

Ma ktoś może funkcję create_date która nie zawiera zmiennych i która nadawałaby się do mojego skryptu?

Tutaj coś znalazłem może kogoś to natchnie, bo ja nie ogarniam tego
http://mody.lastinn.info/funkcja_create_da...hpbb-t1144.html
outsider
  1. function create_date($format, $gmepoch, $tz)
  2. {
  3. return @gmdate($format, $gmepoch + (3600 * $tz));
  4. }
-> to ta funkcja tak naprawde robi, to co wywalilem odpowiadalo za poprawne wyswietlanie daty, chodzi format 'Y m d' lub 'Y d m' itp. (bynajmniej tak bez wiekszej rozkminy rozumiem z tego kodu). Jesli chcesz miec poprawny format musisz dolaczyc do pliku te zmienne globalne/statyczne.
xamrex
No to już sobie prawie poradziłem
  1. <?php
  2.  
  3. function mkrealdate($day,$month,$birth_year)
  4. {
  5. // range check months
  6. if ($month<1 || $month>12) return "error";
  7. // range check days
  8. switch ($month)
  9. {
  10. case 1: if ($day>31) return "error";break;
  11. case 2: if ($day>29) return "error";
  12. $epoch=$epoch+31;break;
  13. case 3: if ($day>31) return "error";
  14. $epoch=$epoch+59;break;
  15. case 4: if ($day>30) return "error" ;
  16. $epoch=$epoch+90;break;
  17. case 5: if ($day>31) return "error";
  18. $epoch=$epoch+120;break;
  19. case 6: if ($day>30) return "error";
  20. $epoch=$epoch+151;break;
  21. case 7: if ($day>31) return "error";
  22. $epoch=$epoch+181;break;
  23. case 8: if ($day>31) return "error";
  24. $epoch=$epoch+212;break;
  25. case 9: if ($day>30) return "error";
  26. $epoch=$epoch+243;break;
  27. case 10: if ($day>31) return "error";
  28. $epoch=$epoch+273;break;
  29. case 11: if ($day>30) return "error";
  30. $epoch=$epoch+304;break;
  31. case 12: if ($day>31) return "error";
  32. $epoch=$epoch+334;break;
  33. }
  34. $epoch=$epoch+$day;
  35. $epoch_Y=sqrt(($birth_year-1970)*($birth_year-1970));
  36. $leapyear=round((($epoch_Y+2) / 4)-.5);
  37. if (($epoch_Y+2)%4==0)
  38. {// curent year is leapyear
  39. $leapyear--;
  40. if ($birth_year >1970 && $month>=3) $epoch=$epoch+1;
  41. if ($birth_year <1970 && $month<3) $epoch=$epoch-1;
  42. } else if ($month==2 && $day>28) return "error";//only 28 days in feb.
  43. //year
  44. if ($birth_year>1970)
  45. $epoch=$epoch+$epoch_Y*365-1+$leapyear;
  46. else
  47. $epoch=$epoch-$epoch_Y*365-1-$leapyear;
  48. return $epoch;
  49. }
  50. function create_date($format, $gmepoch, $tz)
  51. {
  52. global $board_config, $lang;
  53. static $translate;
  54.  
  55. if ( empty($translate) && $board_config['default_lang'] != 'english' )
  56. {
  57. @reset($lang['datetime']);
  58. while ( list($match, $replace) = @each($lang['datetime']) )
  59. {
  60. $translate[$match] = $replace;
  61. }
  62. }
  63.  
  64. return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  65. }
  66. function realdate($date_syntax="Ymd",$date=0)
  67. {
  68. global $lang;
  69. $i=2;
  70. if ($date>=0)
  71. {
  72. return create_date($date_syntax,$date*86400+1,0);
  73. } else
  74. {
  75. $year= -(date%1461);
  76. $days = $date + $year*1461;
  77. while ($days<0)
  78. {
  79. $year--;
  80. $days+=365;
  81. if ($i++==3)
  82. {
  83. $i=0;
  84. $days++;
  85. }
  86. }
  87. }
  88. $leap_year = ($i==0) ? TRUE : FALSE;
  89. $months_array = ($i==0) ?
  90. array (0,31,60,91,121,152,182,213,244,274,305,335,366) :
  91. array (0,31,59,90,120,151,181,212,243,273,304,334,365);
  92. for ($month=1;$month<12;$month++)
  93. {
  94. if ($days<$months_array[$month]) break;
  95. }
  96.  
  97. $day=$days-$months_array[$month-1]+1;
  98. return strtr ($date_syntax, array(
  99. 'a' => '',
  100. 'A' => '',
  101. '\\d' => 'd',
  102. 'd' => ($day>9) ? $day : '0'.$day,
  103. '\\D' => 'D',
  104. 'D' => $lang['day_short'][($date-3)%7],
  105. '\\F' => 'F',
  106. 'F' => $lang['month_long'][$month-1],
  107. 'g' => '',
  108. 'G' => '',
  109. 'H' => '',
  110. 'h' => '',
  111. 'i' => '',
  112. 'I' => '',
  113. '\\j' => 'j',
  114. 'j' => $day,
  115. '\\l' => 'l',
  116. 'l' => $lang['day_long'][($date-3)%7],
  117. '\\L' => 'L',
  118. 'L' => $leap_year,
  119. '\\m' => 'm',
  120. 'm' => ($month>9) ? $month : '0'.$month,
  121. '\\M' => 'M',
  122. 'M' => $lang['month_short'][$month-1],
  123. '\\n' => 'n',
  124. 'n' => $month,
  125. 'O' => '',
  126. 's' => '',
  127. 'S' => '',
  128. '\\t' => 't',
  129. 't' => $months_array[$month]-$months_array[$month-1],
  130. 'w' => '',
  131. '\\y' => 'y',
  132. 'y' => ($year>29) ? $year-30 : $year+70,
  133. '\\Y' => 'Y',
  134. 'Y' => $year+1970,
  135. '\\z' => 'z',
  136. 'z' => $days,
  137. '\\W' => '',
  138. 'W' => '') );
  139. }
  140.  
  141. //cyfra - data
  142. echo realdate('d M Y', 7305);
  143.  
  144. ?>


I wszystko działa jak należy winksmiley.jpg

Tylko pojawił się mały problem,
Za każdym razem na mojej stronie wykonuje się taki warunek (nawet nie wiem co on oznacza; stronę mam na CMSie)
  1. if (!ini_get("register_globals")) {
  2. }


I jeśli takie coś jest w mojej funkcji to nie chce odczytywać mi daty ;(
Co mogę zrobić, aby zaczął ten skrypt działać
//Muszę includować ten warunek, nie ma możliwości abym go wyłączył, więc porady takie jak "Wyłącz ten warunek" nie przejdą.

Innymi słowy co muszę zrobić, aby ten skrypt wraz z tym warunkiem:
  1. if (!ini_get("register_globals")) {
  2. }

zaczął działać?
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.