How to add days or months to any date or today in PHP

2 examples.

Add 10 days from today

$days=10;
$tmpdate=date_create(‘now’);
$tmpdate->add(new DateInterval(‘P’.($days).’D’));
$more10days=$tmpdate->format(‘Y-m-d’);

 

Add 2 months from 13/3/2012 at 12:50 in Madrid

$months=2;
$timeZone = ‘Europe/Madrid’;  // +1 hour
$dateSrc = ‘2012-03-13 12:50’; 
$tmpdate= new DateTime($dateSrc, new DateTimeZone(‘GMT’)); 
$tmpdate->setTimeZone(new DateTimeZone($timeZone)); 
$tmpdate->add(new DateInterval(‘P’.($months).’M’));
$more10days=$tmpdate->format(‘Y-m-d’);

More on DateTime and DateInterval creation

Anuncis

Etiquetes: ,

Deixa un comentari

Fill in your details below or click an icon to log in:

WordPress.com Logo

Esteu comentant fent servir el compte WordPress.com. Log Out / Canvia )

Twitter picture

Esteu comentant fent servir el compte Twitter. Log Out / Canvia )

Facebook photo

Esteu comentant fent servir el compte Facebook. Log Out / Canvia )

Google+ photo

Esteu comentant fent servir el compte Google+. Log Out / Canvia )

Connecting to %s


%d bloggers like this: