Ajouter 1 mois, avec la fonction strtotime

Par 6ber6ou, il y a 13 ans


Salut @ tous !

J'ai un problème avec un fonction qui est censé ajouter 1 mois à une date :

public function add_months($nb_months = 1, $date)
    {
    return date('Y-m-d', strtotime($date.' +'.$nb_months.' month'));
    }

Bizarre non ??

2 réponses

snap, il y a 13 ans

tu veux ajouter 1 mois ou 31 jours?
car pour 31 jours tu remplace strtotime($date.' +'.$nb_months.' month') par strtotime($date) + $nb_months * (3600 * 24 * 31))

Après le mieux serait d'utiliser la classe DateTime (avec sa fonction add() )

6ber6ou, il y a 13 ans

J'ai finalement utilisé DateTime c'est bien plus simple.
Merci pour ton aide