Posted By

snstro on 03/12/15


Tagged

php DateTime days range Datas period interval intevalo


Versions (?)

Show a list of days between two dates


 / Published in: PHP
 

Show a list of days between two dates, using native php DateTime functions

  1. // Mandatory to set the default timezone to work with DateTime functions
  2. date_default_timezone_set('America/Sao_Paulo');
  3.  
  4. $start_date = new DateTime('2010-10-01');
  5. $end_date = new DateTime('2010-10-05');
  6.  
  7. $period = new DatePeriod(
  8. $start_date, // 1st PARAM: start date
  9. new DateInterval('P1D'), // 2nd PARAM: interval (1 day interval in this case)
  10. $end_date, // 3rd PARAM: end date
  11. DatePeriod::EXCLUDE_START_DATE // 4th PARAM (optional): self-explanatory
  12. );
  13.  
  14.  
  15. foreach($period as $date) {
  16. echo $date->format('Y-m-d').'<br/>'; // Display the dates in yyyy-mm-dd format
  17. }

Report this snippet  

You need to login to post a comment.