Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Daty z przedzialu czasu
kiler129
post
Post #1





Grupa: Zarejestrowani
Postów: 566
Pomógł: 35
Dołączył: 21.06.2006

Ostrzeżenie: (0%)
-----


Mam dane 2 daty:

Kod
$data_start = '2008-07-30';
$data_end = '2008-08-3';


Chcialbym aby skrypt wypisal cos takiego:
Kod
2008-07-30
2008-07-31
2008-08-01
2008-08-02
2008-08-03


Szukalem w google ale nic nie znalazlem ciekawego (IMG:http://forum.php.pl/style_emoticons/default/sciana.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
oscar17
post
Post #2





Grupa: Zarejestrowani
Postów: 102
Pomógł: 22
Dołączył: 3.03.2008

Ostrzeżenie: (0%)
-----


Kiedyś napisałem (bądź znalazłem) coś takiego:
  1. <?php
  2. function dates_between_dates($start_date, $end_date, $time_diff='+1 day') {
  3.  
  4. // Standardize date formats
  5. $start_date = date('Y-m-d H:i', strtotime($start_date));
  6. $end_date = date('Y-m-d H:i', strtotime($end_date));
  7.  
  8. if (strtotime($start_date) > strtotime($end_date)) {
  9. return array();
  10. }
  11.  
  12. // initialize array with start date
  13. $return_array = array();//array($start_date);
  14.  
  15. // Initialize loop counter date
  16. $the_date = $start_date;
  17.  
  18. // loop through days
  19. while (date('Y-m-d H:i', strtotime($the_date.$time_diff)) <= $end_date) {
  20. $the_date = date('Y-m-d H:i', strtotime($the_date.$time_diff));
  21. $return_array[] = $the_date;
  22. }
  23.  
  24. return $return_array;
  25. }
  26. ?>

Możesz sobie dodatkowo ustawić co ile ma się zmieniać data - standardowo jest co jeden dzień, ale można rzadziej...

--edit---

A tutaj masz ją przystosowaną pod twoje wymagania
  1. <?php
  2. function dates_between_dates($start_date, $end_date, $time_diff='+1 day') {
  3.  
  4.  // Standardize date formats
  5.  $start_date = date('Y-m-d', strtotime($start_date));
  6.  $end_date = date('Y-m-d', strtotime($end_date));
  7.  
  8. if (strtotime($start_date) > strtotime($end_date)) {
  9. return array();
  10. }
  11.  
  12. // initialize array with start date
  13. $return_array = array($start_date);
  14.  
  15. // Initialize loop counter date
  16. $the_date = $start_date;
  17.  
  18. // loop through days
  19. while (date('Y-m-d', strtotime($the_date.$time_diff)) <= $end_date) {
  20. $the_date = date('Y-m-d', strtotime($the_date.$time_diff));
  21. $return_array[] = $the_date;
  22. }
  23. return $return_array;
  24. }
  25.  
  26. print_r(dates_between_dates('2008-07-30', '2008-08-03'));
  27. ?>


Ten post edytował oscar17 20.08.2008, 07:19:20
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 16.10.2025 - 09:28