5 Replies - 1230 Views - Last Post: 09 December 2012 - 04:44 AM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

datetime format in php

Posted 08 December 2012 - 10:24 PM

i am using xampp. col name "date_time" and its type is "datetime"

ex: 2012-12-07 03:00:00

so i used this in my sql query to store in database
$sql = mysql_query("INSERT INTO item  ...
                   ,date('Y-m-d H:i:s',time()),...



but the query doesnt work. any idea what i did wrong?

This post has been edited by hwoarang69: 08 December 2012 - 10:26 PM

Is This A Good Question/Topic? 0
  • +

Replies To: datetime format in php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3709
  • View blog
  • Posts: 5,954
  • Joined: 08-June 10

Re: datetime format in php

Posted 08 December 2012 - 10:44 PM

The date format you use looks fine. Without seeing more of the actual code, it's not really easy to guess what could be wrong. Perhaps you forgot the quotes around the date value? How, exactly, is it not working? Are you getting any error messages?

With that said, keep in mind that MySQL also has functions specifically designed to provide the current date for it's DATETIME and TIMESTAMP fields. One of those is the NOW() function. It can be used like this:
INSERT INTO `theTable`(`theField`) VALUES(NOW())


Was This Post Helpful? 1
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 925
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: datetime format in php

Posted 08 December 2012 - 10:44 PM

Is that literally what your code is? If it's not can you show us what the query actually is? We're not magic, we can't tell you what the error is without seeing the whole picture.
Was This Post Helpful? 1
  • +
  • -

#4 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: datetime format in php

Posted 08 December 2012 - 11:12 PM

for type "date" i use "now()"
for type "datetime" its "date('Y-m-d H:i:s',time())"
for type "timestamp" no idea.

full query
//add this product into the database now
$sql = mysql_query("INSERT INTO item 
	(image_user_name, image_folder_name, main_category,
          sub_category, price, description, features, key_words, 
          sold, item_add_date)
	
         VALUES('$image_user_name_p','$image_folder_name',
              '$main_category_p','$sub_category_p',
	      '$price_p','$description_p','$feature_p',
'$key_word_p', '0' , date('Y-m-d H:i:s',time())") 
	or die("additem sql_query error"); //date() //date,timestamp



error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'time())' at line 4

This post has been edited by hwoarang69: 08 December 2012 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 925
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: datetime format in php

Posted 09 December 2012 - 02:36 AM

Ah, well there's your problem. You cannot call functions inside of strings.

You can do something like this:

//add this product into the database now
$timestamp = date('Y-m-d H:i:s',time());
$sql = mysql_query("INSERT INTO item 
	(image_user_name, image_folder_name, main_category,
          sub_category, price, description, features, key_words, 
          sold, item_add_date)
	
         VALUES('$image_user_name_p','$image_folder_name',
              '$main_category_p','$sub_category_p',
	      '$price_p','$description_p','$feature_p',
'$key_word_p', '0' , '$timestamp'") 
	or die("additem sql_query error"); //date() //date,timestamp



I would recommend you stop using mysql_* functions and move onto PDO or MySQLi - Both provide more security.
Was This Post Helpful? 1
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3709
  • View blog
  • Posts: 5,954
  • Joined: 08-June 10

Re: datetime format in php

Posted 09 December 2012 - 04:44 AM

View Posthwoarang69, on 09 December 2012 - 06:12 AM, said:

for type "date" i use "now()"
for type "datetime" its "date('Y-m-d H:i:s',time())"
for type "timestamp" no idea.

The NOW() functions works fine for all three. There is no need to be creating the date string in PHP like you are doing.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1