9 de diciembre de 2011

Add days to date in PHP

La siguiente función sumará un número determinado de días a un fecha dada.

/**
* Add days to a date.
*
* @param string $d, date
* @param int $ndays, number of days
* @param string $format, format date (@see: http://php.net/manual/en/function.date.php)
*/
function add_days($ndays, $d, $format="Y-m-d") {
$p = $ndays > 1 ? "s" : "";
$nf = strtotime(date($format, strtotime($d)) . " +".$ndays." day". $p);
return date($format, $nf);
}
view raw add_days.php hosted with ❤ by GitHub

Referencias:
http://www.vision.to/how-to-add-days-weeks-months-to-any-date-.php
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php

No hay comentarios:

Publicar un comentario