Fechas entre dos fechas


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

Obtener las fechas que hay entre dos fechas dadas. No es la diferencia de días o similares, sino las fechas en sí.


Copy this code and paste it in your HTML
  1. /**
  2.  * Returns an array with the dates between to dates given.
  3.  *
  4.  * @link http://us3.php.net/manual/en/function.date.php#AEN25217
  5.  *
  6.  * @param mixed $startdate Timestamp or strtotime() recognizeable string
  7.  * @param mixed $enddate Timestamp or strtotime() recognizeable string
  8.  * @param string[optional] $format date() format string
  9.  * @return mixed Array of timestamps or dates if given format
  10.  */
  11. public static function dates_between($startdate, $enddate, $format=null){
  12.  
  13. (is_int($startdate)) ? 1 : $startdate = strtotime($startdate);
  14. (is_int($enddate)) ? 1 : $enddate = strtotime($enddate);
  15.  
  16. if($startdate > $enddate){
  17. return false; //The end date is before start date
  18. }
  19.  
  20. while($startdate < $enddate){
  21. $arr[] = ($format) ? date($format, $startdate) : $startdate;
  22. $startdate += 86400;
  23. }
  24. $arr[] = ($format) ? date($format, $enddate) : $enddate;
  25.  
  26. return $arr;
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.