i am using xammp for my database.
i have a col called "today_date" andy type "DATE".
in php i am using function "now()" to store todays date. the format is year-month-day.

now let say i have one more col called "today_date_time" and type "datetime".
is there a php function that can get me a date and time of today and format is year-month-day-hour-min-sec

Dani AI

Generated

Short answer: build a proper timestamp in PHP, put it into a variable, then insert that variable into the query (preferably with prepared statements). A DATE column only stores a date-only value; to keep hours, minutes and seconds use a DATETIME or TIMESTAMP column. The forum replies from , and already point out the timezone, DB-side timestamp and "compute-first" ideas — below is a safe, modern example and a short troubleshooting checklist.

// create a timezone-aware timestamp in PHP and insert with PDO
$dt = new DateTime('now', new DateTimeZone('UTC'));   // choose the timezone you need
$timestamp = $dt->format('Y-m-d H:i:s');

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$sql = 'INSERT INTO item (image_user_name, image_folder_name, main_category, sub_category, price, description, features, key_words, sold, item_add_date)
        VALUES (:name, :folder, :main, :sub, :price, :desc, :feat, :keys, :sold, :date)';
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':name'   => $image_user_name_p,
    ':folder' => $image_folder_name,
    ':main'   => $main_category_p,
    ':sub'    => $sub_category_p,
    ':price'  => $price_p,
    ':desc'   => $description_p,
    ':feat'   => $feature_p,
    ':keys'   => $key_word_p,
    ':sold'   => 0,
    ':date'   => $timestamp,
]);

Troubleshooting checklist

  • The original SQL syntax error came from embedding a PHP expression inside the quoted SQL string; compute the timestamp in PHP first or bind it as a parameter instead.
  • Ensure the column type accepts time (DATETIME/TIMESTAMP, not DATE).
  • Decide where timestamps are generated: PHP (gives control over timezone) or the DB (automatic defaults). rightly noted timezone mismatch can confuse values; pick UTC for consistency or explicitly set a timezone.
  • Avoid the old mysql_* API; use mysqli or PDO and parameter binding to prevent injection and syntax issues.
  • If still failing, log the final SQL and the DB error message, and confirm the column definitions with SHOW COLUMNS.

Recommended Answers

All 7 Replies

Member Avatar for Member #671080

Is this what you mean?

putenv("TZ=Europe/London"); // Set to your timezone (server may be in a different zone).
$now_datetime = date("Y-m-d-H-i-s", time()); // eg. 2012-12-09-03-34-25

i tryied this date('Y-m-d H:i:s',time()) but didnt worked. it does print
eg 2012-12-09 03:34:25. but i cant seem to store in database.

$sql = mysql_query("INSERT INTO item  ...
                   ,date('Y-m-d H:i:s',time()),...

i will not use php ever, I will use mysql stuff to add time and date

insert into table1 (col1,col2,today_date,today_datetime) values ('col1value','col2value',current_date(),current_timestamp())

error:
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

$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(mysql_error()); //date() //date,timestamp

try without braces
current_date, current_timestamp

it does print eg 2012-12-09 03:34:25. but i cant seem to store in database

What is the format of the field you want to store the value into?

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

PHP functions can not be parsed within a double quoted string (as far as I know). Assign a value to a variable first and then use this variable in a query.

$temp_time = 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' , '$temp_time')")
or die(mysql_error()); //date() //date,timestamp
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.