Phil Young
Phil Young

Reputation: 1354

Inserting or updating mysql datetime field with php

I have a function which adds an item to the basket. In this function, it checks to see if a basket already exists for this user and creates or updates the basket accordingly. When creating or updating the basket, I need a timeout field set to 4 hours in the future. However, when I create or update a basket, the dateTime field for the timeout is at 0000-00-00 00:00:00. Here is my code (php using the codeigniter framework):

$dateTime = date("Y-d-m h:m:s", strtotime("+4 hours"));

if($query->num_rows() != 1)
{


    $this->db->insert("baskets", array("basket_session" => session_id(), "basket_timeout" => $dateTime, "basket_delivery" => "0.00"));

}
else
{

    $this->db->where("basket_session", session_id());
    $this->db->update("baskets", array("basket_timeout" => $dateTime));

}

What am I missing?

Upvotes: 1

Views: 7262

Answers (3)

Dr. Dan
Dr. Dan

Reputation: 2288

Change

$dateTime = date("Y-d-m h:m:s", strtotime("+4 hours"));

to

$dateTime = date("Y-m-d H:i:s", strtotime("+4 hours"));

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

TRy:

$dateTime = date("Y-m-d H:i:s", strtotime("+4 hours"));

Upvotes: 1

deceze
deceze

Reputation: 521995

The DATETIME format for MySQL is Y-m-d H:i:s.

Upvotes: 4

Related Questions