PHP date () - insert into MySQL database

I have a bit of trouble getting the added date in a MySQL database. The field is of type DATE.

First of all, I increment the date with the following code:

if($_POST['membershipLength'] == "6 Months") {
    $renew = mktime(0, 0, 0, date("m")+9, date("d"), date("y"));
}

Then I want to type: date("m/d/y", $renew)into a column in the database.

Can anyone see where I'm wrong? The value it inserts consists of 0, which I accept by default.

+3
source share
5 answers

The MYSQL date field has the following format "Ymd", something like "2010-11-04". You are trying to insert a value like "2010/04/11". Change your first parameter to "Ymd" and it should work :)

+5
source

:

date ("Y-m-d H:i:s", $renew);

MySQL .

+2

MySQL - YYYY-MM-DD, M-D-Y Y-M-D, .

+2

I tried your code and worked fine except for one. If you want to add 6 months, why do you add 9 months?

if($_POST['membershipLength'] == "6 Months") {



        $date = mktime(0, 0, 0, date("m")+6, date("d"), date("Y"));

        $renew = date("Y-m-d", $date);



    } elseif($_POST['membershipLength'] == "9 Months") {



        $date = mktime(0, 0, 0, date("m")+9, date("d"), date("Y"));

        $renew = date("Y-m-d", $date);



    } elseif($_POST['membershipLength'] == "12 Months") {



        $date = mktime(0, 0, 0, date("m")+12, date("d"), date("Y"));

        $renew = date("Y-m-d", $date);



    }
+1
source

Thanks for all your help. The error I made, in addition, all the corrections that you all proposed, was that I checked a value that did not match. Stupid me!

+1
source

Source: https://habr.com/ru/post/1773028/


All Articles