Can someone please help:(. I have a database with a field Date. I made the datatype to Date. But when I code the date in php I use the date() function to get the current date it donesn't want to insert in the database. Please help.

For example my code.

$name = $_POST['name'];
$date = date("yyyy,mm,dd");

insert into tablename ('name','date') values ('{$name}','{$date}');

Dani AI

Generated

A few quick fixes and why they matter.

The most common causes here are: using the wrong PHP format string, using single quotes around column names in the INSERT (those become string literals), or the query failing silently because errors were not checked. was on the right track suggesting a server-side function; was right that PHP format tokens are needed — but the output must match MySQL’s DATE format (YYYY-MM-DD).

Correct workflow (PHP side): produce a string in the MySQL DATE format and send it with a parameterized query. Example using PDO:

$date = date("Y-m-d");          // yields 2025-11-14
$stmt = $pdo->prepare(
  "INSERT INTO `tablename` (`name`, `date`) VALUES (:name, :date)"
);
$stmt->execute(['name' => $name, 'date' => $date]);

If you prefer the database to set the date, use MySQL’s date function for a DATE column (server time), for example via CURDATE() in the INSERT. That avoids PHP timezone issues but remember NOW() returns DATETIME while CURDATE() returns DATE; pick the one that matches the column type.

Always:

  • Access form values correctly (e.g., $_POST['name']).
  • Use parameterized queries to avoid SQL injection.
  • Check and log the database error if the query fails (mysqli_error, PDOException, etc.).
  • Avoid naming a column literally date if it causes confusion; use created_at or quote identifiers with backticks.

PHP date() format reference: date()
MySQL date/time docs (types and functions): MySQL Date and Time

Recommended Answers

All 2 Replies

Can someone please help:(. I have a database with a field Date. I made the datatype to Date. But when I code the date in php I use the date() function to get the current date it dones want to insert in the database. Please help.

For example my code.

$name = $_POST;
$date = date("yyyy,mm,dd");

insert into tablename ('name','date') values ('{$name}','{$date}');

no need of date field
just copy like this.

insert into tablename ('name','date') values ('{$name}',now());

take date in datebase

Can someone please help:(. I have a database with a field Date. I made the datatype to Date. But when I code the date in php I use the date() function to get the current date it donesn't want to insert in the database. Please help.

For example my code.

$name = $_POST;
$date = date("yyyy,mm,dd");

insert into tablename ('name','date') values ('{$name}','{$date}');

use it this way -

date('Y,m,d');
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.