Question

I've been looking everywhere for information on how to add an amount of time (months) specified in a form on a Php website to current date, and then adding this date into MySQL database Date (for ex. 2014-03-03) field (if using Date is even possible here).

Could You tell me what should I use to take the current date without time, add a custom amount of months to it (using strtotime I assume, but it uses syntax like +1 days, where I have a variable from a form), and then input it into database field type "Date"? I could change the DB field type to Datetime or Timestamp if needed.

Was it helpful?

Solution

You can use mktime for this:

<?php

print date("Y-m-d", mktime(0, 0, 0, date("m") + 6, date("d"), date("Y"))); 

Here, I have added 6 months to the date.

OTHER TIPS

following this logic it should be pretty easy to save your date in mysql. =) strtotime is a great function because its always very easy to understand what's going on at a glance.

$days = $_POST['days'];  // data from your form

$currentTime = strtotime('today');  // you can also use time() or put in your own date
$nextDay = strtotime('+'.$days.' days', $currentTime);  // add number of days from form
$nextDay = date('Y-m-d H:i:s', $currentTime);  // format the time from unix timestamp into mysql format
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top