user484002
user484002

Reputation:

PHP date() - inserting into an MySQL database

I'm having a little bit of trouble with getting an incremented date into an 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 wish to enter: date("m/d/y", $renew) into the column in the database.

Can anyone see where I'm going wrong? The value it inserts consists of 0s which I'm assuming is the default.

Upvotes: 3

Views: 3660

Answers (5)

user484002
user484002

Reputation:

Thanks for your help everyone. The mistake I was making in addition the corrections you all suggested was that I was checking for a value that didn't match. Silly me!

Upvotes: 1

Gajendra Bang
Gajendra Bang

Reputation: 3583

I tried your code and working fine, other than one thing. When you want to add 6 months, why are you adding 9 in 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);



    }

Upvotes: 1

Gazillion
Gazillion

Reputation: 4792

The MYSQL date field has the following format "Y-m-d" so something like "2010-11-04". You're trying to insert a value as "2010/04/11". Change your first parameter to "Y-m-d" and it should work :)

Upvotes: 5

Badr
Badr

Reputation: 10668

the date format in MySQL is YYYY-MM-DD and you are inserting M-D-Y try with Y-M-D it will resolve your problem

Upvotes: 2

Surreal Dreams
Surreal Dreams

Reputation: 26380

Try this:

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

MySQL likes dates in a specific format.

Upvotes: 2

Related Questions