Frank
Frank

Reputation: 35

Calculate a date with PHP

I am using an html form to get a user inputted date. The structure of the date inputted is: MM/DD/YYYY. I then need to increment the total days by 196 in PHP. Right now, the data is being posted to a php file called Calculate.php. I was looking into altering the data using date (m d Y); in php, but my friend said that probably wont work. Any ideas? Thank you for your time and have a great day! ^_^

Upvotes: 1

Views: 303

Answers (3)

Brad Christie
Brad Christie

Reputation: 101604

  1. Check out strtotime. This will convert your MM/DD/YYYY format in to a numeric value you can then work with.
  2. Use strtotime again to manipulate the date to add the days to it.
  3. Use strftime to re-format it for display.

e.g.

$d = '08/11/2011';

$dAsPOSIX = strtotime($d);
$dPlus196Days = strtotime('+196 day', $dAsPOSIX);

echo strftime('%m/%d/%Y',$dPlus196Days);

DEMO

Upvotes: 2

Alex Turpin
Alex Turpin

Reputation: 47776

Using strtotime magic:

strtotime('08/11/2011 +196 days');

Upvotes: 2

Dan Grossman
Dan Grossman

Reputation: 52372

$input = $_POST['date'];

echo date('m d Y', strtotime('+196 days', strtotime($input));

Upvotes: 0

Related Questions