2015-02-05 10 views
6

Se uso questo codice ottengo risultati strani:PHP Febbraio data: "2015-01-31" +1 mese: "2015-03-30". Come risolvere?

$datetime = new DateTime('2015-01-31'); 
$datetime->modify('+1 month'); 
echo $datetime->format('Y-m-t') . "<br>"; 
$datetime->modify('+1 month'); 
echo $datetime->format('Y-m-t') . "<br>"; 
$datetime->modify('+1 month'); 
echo $datetime->format('Y-m-t') . "<br>"; 

ottengo questo:

2015-03-31 
2015-04-30 
2015-05-31 

E non 2015-02-28.

Come risolvere?

+0

@all: si trova questa s un vero bug? Ciò sta chiaramente rompendo l'usabilità e rompendo definitivamente molte applicazioni. – Sliq

risposta

0

Se si desidera ottenere l'ultimo giorno del mese successivo, è possibile utilizzare:

$datetime->modify('last day of next month'); 
0

Fix in questo modo.

$datetime = new DateTime('2015-01-31'); 
$datetime->modify('28 days'); 
echo $datetime->format('Y-m-t') . "<br>"; 
$datetime->modify('+1 month'); 
echo $datetime->format('Y-m-t') . "<br>"; 
$datetime->modify('+1 month'); 
echo $datetime->format('Y-m-t') . "<br>"; 

Otterrete

2015-02-28 
2015-03-31 
2015-04-30 
+2

Sei un genio, davvero !! GENIO!!! GENIO!!! –

+0

Non proprio .. questo non funziona in generale. Solo se sai già che hai a che fare con gennaio, e poi, beh, perché usare "DateTime" per portarti a febbraio? Molto meglio scegliere una soluzione semplice che funziona sempre e sembra sempre la stessa, come mopo pubblicato. –

+0

Sto scherzando quando dico che sei un genio. –

3

Il modo DateTime opere, + 1 month aumenta il valore del mese per uno, dando 2015-02-31. Poiché ci sono solo 28 o 29 giorni a febbraio, questo valuterà fino ai primi giorni di marzo. Quindi, come sai, chiedere Y-m-t ti darà l'ultimo giorno di marzo.

Dal momento che si sta già utilizzando t per ottenere l'ultimo giorno del mese, è possibile evitare questo problema partendo con una data che cade all'inizio di un mese invece:

$datetime = new DateTime('2015-01-01'); 

di riferimento : PHP DateTime::modify adding and subtracting months

0

Si può provare questa funzione per aggiungere mesi a un oggetto datetime

/** 
* 
* @param \DateTime $date DateTime object 
* @param int $monthToAdd Months to add at time 
*/ 
function addMonth(\DateTime $date, $monthToAdd) 
{ 
    $year = $date->format('Y'); 
    $month = $date->format('n'); 
    $day = $date->format('d'); 

    $year += floor($monthToAdd/12); 
    $monthToAdd = $monthToAdd % 12; 
    $month += $monthToAdd; 
    if ($month > 12) { 
     $year ++; 
     $month = $month % 12; 
     if ($month === 0) { 
      $month = 12; 
     } 
    } 

    if (! checkdate($month, $day, $year)) { 
     $newDate = \DateTime::createFromFormat('Y-n-j', $year . '-' . $month . '-1'); 
     $newDate->modify('last day of'); 
    } else { 
     $newDate = \DateTime::createFromFormat('Y-n-d', $year . '-' . $month . '-' . $day); 
    } 
    $newDate->setTime($date->format('H'), $date->format('i'), $date->format('s')); 

    return $newDate->format('Y-m-d'); 
} 

echo addMonth(new \DateTime('2015-01-30'), 1); //2015-02-28 
echo addMonth(new \DateTime('2015-01-30'), 2); //2015-03-30 
echo addMonth(new \DateTime('2015-01-30'), 3); //2015-04-30 
Problemi correlati