Show a list of days between two dates


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.