Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]lista dat w podanym zakresie
Forum PHP.pl > Forum > Przedszkole
northwest
Witam serdecznie,
Potrzebuję skrypt do wyświetlenia dat pomiędzy 2 datami. Czyli przykładowo podaję datę:
2015-01-01 oraz 2015-01-05 i skrypt zwraca:
2015-01-01
2015-01-02
2015-01-03
2015-01-04
2015-01-05


Wie ktoś może jak to zrobić w php?smile.gif


Bardzo proszę o pomoc,
Northwest
skowron-line
  1. <?php
  2. $start = '2009-10-11';
  3. $datetime1 = new DateTime($start);
  4. $datetime2 = new DateTime('2009-10-13');
  5. $interval = $datetime1->diff($datetime2)->d;
  6.  
  7. $d = [];
  8. for($i = 0; $i<= $interval; $i++) {
  9. $d[] = (new DateTime($start))->add(new \DateInterval('P'. $i .'D'));
  10. }
  11.  

masz gotowca
northwest
Dziękuję bardzo smile.gif

orientujesz się może czy da się pobrać z tego wyniku tylko i wyłącznie "czyste" daty:
  1.  
  2. array(22) {
  3. [0]=>
  4. object(DateTime)#5 (3) {
  5. ["date"]=>
  6. string(26) "2015-04-01 00:00:00.000000"
  7. ["timezone_type"]=>
  8. int(3)
  9. ["timezone"]=>
  10. string(13) "Europe/Berlin"
  11. }
  12. [1]=>
  13. object(DateTime)#6 (3) {
  14. ["date"]=>
  15. string(26) "2015-04-02 00:00:00.000000"
  16. ["timezone_type"]=>
  17. int(3)
  18. ["timezone"]=>
  19. string(13) "Europe/Berlin"
  20. }
  21. [2]=>
  22. object(DateTime)#7 (3) {
  23. ["date"]=>
  24. string(26) "2015-04-03 00:00:00.000000"
  25. ["timezone_type"]=>
  26. int(3)
  27. ["timezone"]=>
  28. string(13) "Europe/Berlin"
  29. }
  30. [3]=>
  31. object(DateTime)#8 (3) {
  32. ["date"]=>
  33. string(26) "2015-04-04 00:00:00.000000"
  34. ["timezone_type"]=>
  35. int(3)
  36. ["timezone"]=>
  37. string(13) "Europe/Berlin"
  38. }
  39. [4]=>
  40. object(DateTime)#9 (3) {
  41. ["date"]=>
  42. string(26) "2015-04-05 00:00:00.000000"
  43. ["timezone_type"]=>
  44. int(3)
  45. ["timezone"]=>
  46. string(13) "Europe/Berlin"
  47. }
  48. [5]=>
  49. object(DateTime)#10 (3) {
  50. ["date"]=>
  51. string(26) "2015-04-06 00:00:00.000000"
  52. ["timezone_type"]=>
  53. int(3)
  54. ["timezone"]=>
  55. string(13) "Europe/Berlin"
  56. }
  57. [6]=>
  58. object(DateTime)#11 (3) {
  59. ["date"]=>
  60. string(26) "2015-04-07 00:00:00.000000"
  61. ["timezone_type"]=>
  62. int(3)
  63. ["timezone"]=>
  64. string(13) "Europe/Berlin"
  65. }
  66. [7]=>
  67. object(DateTime)#12 (3) {
  68. ["date"]=>
  69. string(26) "2015-04-08 00:00:00.000000"
  70. ["timezone_type"]=>
  71. int(3)
  72. ["timezone"]=>
  73. string(13) "Europe/Berlin"
  74. }
  75. [8]=>
  76. object(DateTime)#13 (3) {
  77. ["date"]=>
  78. string(26) "2015-04-09 00:00:00.000000"
  79. ["timezone_type"]=>
  80. int(3)
  81. ["timezone"]=>
  82. string(13) "Europe/Berlin"
  83. }
  84. [9]=>
  85. object(DateTime)#14 (3) {
  86. ["date"]=>
  87. string(26) "2015-04-10 00:00:00.000000"
  88. ["timezone_type"]=>
  89. int(3)
  90. ["timezone"]=>
  91. string(13) "Europe/Berlin"
  92. }
  93. [10]=>
  94. object(DateTime)#15 (3) {
  95. ["date"]=>
  96. string(26) "2015-04-11 00:00:00.000000"
  97. ["timezone_type"]=>
  98. int(3)
  99. ["timezone"]=>
  100. string(13) "Europe/Berlin"
  101. }
  102. [11]=>
  103. object(DateTime)#16 (3) {
  104. ["date"]=>
  105. string(26) "2015-04-12 00:00:00.000000"
  106. ["timezone_type"]=>
  107. int(3)
  108. ["timezone"]=>
  109. string(13) "Europe/Berlin"
  110. }
  111. [12]=>
  112. object(DateTime)#17 (3) {
  113. ["date"]=>
  114. string(26) "2015-04-13 00:00:00.000000"
  115. ["timezone_type"]=>
  116. int(3)
  117. ["timezone"]=>
  118. string(13) "Europe/Berlin"
  119. }
  120. [13]=>
  121. object(DateTime)#18 (3) {
  122. ["date"]=>
  123. string(26) "2015-04-14 00:00:00.000000"
  124. ["timezone_type"]=>
  125. int(3)
  126. ["timezone"]=>
  127. string(13) "Europe/Berlin"
  128. }
  129. [14]=>
  130. object(DateTime)#19 (3) {
  131. ["date"]=>
  132. string(26) "2015-04-15 00:00:00.000000"
  133. ["timezone_type"]=>
  134. int(3)
  135. ["timezone"]=>
  136. string(13) "Europe/Berlin"
  137. }
  138. [15]=>
  139. object(DateTime)#20 (3) {
  140. ["date"]=>
  141. string(26) "2015-04-16 00:00:00.000000"
  142. ["timezone_type"]=>
  143. int(3)
  144. ["timezone"]=>
  145. string(13) "Europe/Berlin"
  146. }
  147. [16]=>
  148. object(DateTime)#21 (3) {
  149. ["date"]=>
  150. string(26) "2015-04-17 00:00:00.000000"
  151. ["timezone_type"]=>
  152. int(3)
  153. ["timezone"]=>
  154. string(13) "Europe/Berlin"
  155. }
  156. [17]=>
  157. object(DateTime)#22 (3) {
  158. ["date"]=>
  159. string(26) "2015-04-18 00:00:00.000000"
  160. ["timezone_type"]=>
  161. int(3)
  162. ["timezone"]=>
  163. string(13) "Europe/Berlin"
  164. }
  165. [18]=>
  166. object(DateTime)#23 (3) {
  167. ["date"]=>
  168. string(26) "2015-04-19 00:00:00.000000"
  169. ["timezone_type"]=>
  170. int(3)
  171. ["timezone"]=>
  172. string(13) "Europe/Berlin"
  173. }
  174. [19]=>
  175. object(DateTime)#24 (3) {
  176. ["date"]=>
  177. string(26) "2015-04-20 00:00:00.000000"
  178. ["timezone_type"]=>
  179. int(3)
  180. ["timezone"]=>
  181. string(13) "Europe/Berlin"
  182. }
  183. [20]=>
  184. object(DateTime)#25 (3) {
  185. ["date"]=>
  186. string(26) "2015-04-21 00:00:00.000000"
  187. ["timezone_type"]=>
  188. int(3)
  189. ["timezone"]=>
  190. string(13) "Europe/Berlin"
  191. }
  192. [21]=>
  193. object(DateTime)#26 (3) {
  194. ["date"]=>
  195. string(26) "2015-04-22 00:00:00.000000"
  196. ["timezone_type"]=>
  197. int(3)
  198. ["timezone"]=>
  199. string(13) "Europe/Berlin"
  200. }
  201. }
  202.  
skowron-line
  1. $d[] = (new DateTime($start))->add(new \DateInterval('P'. $i .'D'))->format('d.m.Y');


edit
tak bedzie ladniej
  1. <?php
  2. $start = '2009-10-11';
  3. $datetime1 = new DateTime($start);
  4. $datetime2 = new DateTime('2009-10-13');
  5. $interval = $datetime1->diff($datetime2)->d;
  6.  
  7. $d = [];
  8. for($i = 0; $i<= $interval; $i++) {
  9. $d[] = $datetime1->add(new \DateInterval('P'. $i .'D'))->format('d.m.Y');
  10. }
  11.  
viking
  1. $d = new \DateTime();
  2. var_dump($d->format(\DateTime::ISO8601));
northwest
działa,dziękuje smile.gif

Kurczę, jednak nie bardzo działa sad.gif

Na localhoście działało mi poprawnie, jednak na home.pl zwraca mi błąd: Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /rezerwacje/index.php on line 591
w tym: $wynikowedaty = (new DateTime($start))->add(new \DateInterval('P'. $i .'D'))->format('Y-m-d');

skowron-line
uuuuu Panie zmień hosting. home.pl ma pewnie jeszcze php 5.3
northwest
Możliwe .... kurcze, zrobiłem sobie na localhoście praktycznie wszystko co chciałem i teraz wysypało mi się przez tego home :/

Masz może pomysł jak to zmienić żeby zadziałało?
com
@up masz inaczej zapisane zrób tak smile.gif btw jaka wersja php na home?

skowron-line to już przejdzie (PHP 5 >= 5.3.0), ale może maja jeszcze 5.2 biggrin.gif
northwest
w panelu home mam zapisane "php w wersji 5.3"
skowron-line
Jezeli dobrze pamiętam to zeby mieć php 5.5 to trzeba było na homie w .htaccess wpisać coś (ale moge się mylić) poszukaj w necie a na przyszłość unikaj home.pl i nazwa.pl tam php 5.3 kroluje.
northwest
nie mam innego hostingu :-(
Dodałem ten wpis o którym piszesz i teraz są jeszcze większe cyrki - przykładowo: Parse error: syntax error, unexpected 'begin' (T_STRING) in /images/3_n_29_03.png on line 4 ;-)
Mógłbym prosić o dostosowanie tego skryptu do tego php 5.3?

Dziękuję bardzo za pomoc.
vokiel
Instrukcja https://pomoc.home.pl/baza-wiedzy/ustawieni...omoca-htaccess/
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.