Foros del Web » Programando para Internet » PHP »

Listar años entre fechas

Estas en el tema de Listar años entre fechas en el foro de PHP en Foros del Web. Hola , tengo el siguiente codigo : Código: <?php $start = new DateTime('2010-12-02'); $start->modify('first day of this month'); $end = new DateTime('2016-05-06'); $end->modify('first day of ...
  #1 (permalink)  
Antiguo 01/02/2016, 11:48
 
Fecha de Ingreso: noviembre-2014
Mensajes: 46
Antigüedad: 9 años, 4 meses
Puntos: 0
Listar años entre fechas

Hola , tengo el siguiente codigo :

Código:
<?php

$start    = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end      = new DateTime('2016-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 year');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y") . "<br>\n";
}

?>
Devuelve :

Código:
2010
2011
2012
2013
2014
2015
¿ Como puedo hacer para que devuelve tambien 2016 ? Deberia devolverlo como lo hace con el 2010
  #2 (permalink)  
Antiguo 01/02/2016, 20:09
Avatar de xfxstudios  
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 9 meses
Puntos: 263
Respuesta: Listar años entre fechas

si colocas los intervalos de fecha de esta manera te da el resultado, ahora si explicas de donde salen las fechas, se podria mejorar:

Código PHP:
Ver original
  1. $start    = new DateTime('2010-01-01');
  2. $start->modify('first day of this month');
  3. $end      = new DateTime('2016-01-01');
  4. $end->modify('first day of next month');
  5. $interval = DateInterval::createFromDateString('1 year');
  6. $period   = new DatePeriod($start, $interval, $end);
  7.  
  8. foreach ($period as $dt) {
  9.     echo $dt->format("Y") . "<br>\n";
  10. }
__________________
[email protected]
HITCEL

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:10.