2

I have a form with the input type date. Now, I want to get the selected date from the form, add 30 day's and save to my MySQL database. Here is an example.

I want from this --> 2014-06-20

too this one --> 2014-07-20 (+30days)

Here is my HTML:

<label for ="lb_Date">Select your Date </label>
<input type="date" class="form-control" id="DateInput" name="date"placeholder="" required>

Here is my PHP

$date = $_POST['date'];
$newDate = new DateTime('$date');
$newDate->modify('+30 day');
echo $date->format('YY-mm-dd');

The echo gives me the right result, but the error message doesn't allow to save $newDate in my MySQL database.

The Error Message:

Catchable fatal error: Object of class DateTime could not be converted to string enter code here...

What's wrong? Why I need to convert something, the output is just right for MySQL. I don't understand.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Haris Bjelic
  • 117
  • 1
  • 9
  • 1
    `$newDate = new DateTime('$date');` inside it is a string literal, not the value of the variable – user1978142 Jun 29 '14 at 04:15
  • "The echo gives me the right result"...?? The code you provided cannot possibly work. Please clarify or fix the code in your question. – Mark Miller Jun 29 '14 at 04:19
  • 1
    This question is valid, but the HTML has nothing to do with it. Let along it being HTML5. It’s strictly a date formatting issue. – Giacomo1968 Jun 29 '14 at 04:27
  • send us your mysql part of the code.. and we can help you out here.. – J-D Jun 29 '14 at 04:46

3 Answers3

1

What you're doing is feeding the datetime object with a string literal, not the value of the variable from your $_POST:

$newDate = new DateTime('$date');

I don't know if thats a typo, but you should change it to this: Example:

$date = $_POST['date'];
$newDate = new DateTime($date); // remove those quotations
$newDate->modify('+30 day');
// should not be YY-mm-dd but Y-m-d
echo $newDate->format('Y-m-d');// 2014-07-20
// not echo $date->format('YY-mm-dd'); $date is not your datetime object
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Hi! It just works, but when I want to save the result ($newDate) in my mysql database then I get the same error. Here is my Code: $sql_insert_coupon = "INSERT INTO coupon ( cashed, valid_from, valid_to, coupon_code) VALUES ( NULL, '$date', '$newDate','$couponCode');"; mysql_query($sql_insert_coupon);.. When I insert the date from the POST in valid_from it just works, but when I add the days to newDate it just come the same error message. – Haris Bjelic Jun 29 '14 at 17:27
  • @user3787069 your giving the database the datetime object not the formatted date, put it inside a variable first, `$final_date = $newDate->format('Y-m-d');`, then insert it to database, `$sql_insert_coupon = "INSERT INTO coupon ( cashed, valid_from, valid_to, coupon_code) VALUES ( NULL, '$date', '$final_date','$couponCode');";` – user1978142 Jun 29 '14 at 21:48
0

Your PHP section should be as follows..

$date = $_POST['date'];
$newDate = new DateTime($date);
$newDate->modify('+30 day');
echo $newDate->format('Y-m-d');
J-D
  • 737
  • 1
  • 12
  • 27
0

Your PHP code should be like this:

$date = $_POST['date'];
$newDate = new DateTime('$date');
$newDate->modify('+30 day');
echo $newDate->format('Y-m-d');

You were calling a method to a string ($_POST['date']) and not an object.

Nahiyan
  • 510
  • 5
  • 19