Posted By

sendoa on 07/07/06


Tagged

fechas


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

postNuKe
willcodeforfood


Fechas entre dos fechas


 / Published in: PHP
 

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

  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  

You need to login to post a comment.