Sahasrangshu Guha
Sahasrangshu Guha

Reputation: 683

adding days with a date in php

In my project i have to get the expiry date of a package from the 'date of purchasing' the package and the 'validity in days' of the package.I got a code from the php.net as follows

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>

Now suppose the date of purchase is like 2013-01-17 and the validity in days is 180 Now if i use the php.net example as follows

<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>

Will i get the output as 2013-04-17? I need the output like

yyyy-mm-dd

I cant run the program in my personal computer but i have to carry on the codding of the project so i'm here.

Upvotes: 1

Views: 214

Answers (1)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

<?php
 $date = new DateTime('2013-01-17');
 $date->add(new DateInterval('P180D'));
 echo $date->format('Y-m-d') . "\n";
?>

//output
//2013-07-16   and it is in yyyy-mm-dd format

working example http://codepad.viper-7.com/gAtT2D

and if you can't run the program in your PC, use online debugger shell.

Upvotes: 4

Related Questions