Can someone help me entering a date "manually" in MySQL thanks :) here's my code it's currently entering 0000-00-00 in my databse

THE HTML

<html>
<head>
<title>..</title>
<body>
<form action="work1.php" method="post">
Value1: <input type="text" name="date"><br>
<input type="Submit">
</form>
</body>
</head>
</html>

PHP

<?php
$username="root";
$password="";
$database="wapwap";

$date=$_POST['Value1'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO date VALUES
('','$date')";

mysql_query($query);

mysql_close();
?>

NEW to PHP :)

Dani AI

Generated

A DATE column in MySQL expects the canonical format YYYY-MM-DD. If the string you insert does not parse as a valid date, MySQL (when not in strict mode) can coerce it to 0000-00-00. Also, strtotime() returns false for unparseable strings; passing that to date() yields 1970-01-01 (Unix epoch). Validate the exact format server-side, and insert using prepared statements.

Example with strict validation and PDO:

$raw = $_POST['date'] ?? '';
$dt = DateTime::createFromFormat('Y-m-d', $raw);
if (!$dt || $dt->format('Y-m-d') !== $raw) {
    die('Invalid date; expected YYYY-MM-DD.');
}

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

$stmt = $pdo->prepare('INSERT INTO yourtable (the_date) VALUES (:d)');
$stmt->execute([':d' => $dt->format('Y-m-d')]);

Additional tips:

  • Ensure your input name matches the POST key and check for empties before using it.
  • Store dates as DATE in the database, not as strings like m/d/Y. See MySQL temporal types.
  • Enable SQL strict mode so invalid dates fail with an error instead of becoming 0000-00-00. Configure sql_mode (e.g., STRICT_TRANS_TABLES) at the server level; see MySQL SQL modes.
  • If you only need the current date, insert CURDATE() directly in SQL instead of passing it from PHP.

References: PHP DateTime::createFromFormat.

Recommended Answers

All 10 Replies

Your Date is coming as a String.Due to this , it is displaying 0000-00-00.

Solution is.....

$date=$_POST['Value1'];

---- This is the String.
After taking the date value as a string convert the string with date format.

Member Avatar for Member #334542

You should use the name of the element not the label

Day:<input type="text" name="dy"><br>
Month:<input type="text" name="mth"><br>
Year:<input type="text" name="yr"><br>

in PHP

$dy = $_POST['dy'];
$mth=$_POST['mth'];
$yr = $_POST['yr'];
$phpdate = $yr."-".$mth."-".$dy;
$phpdate=date($phpdate);

@Ankit Parmar: Exactly how will i do it? i've searched strtotime() but can you show me how?

After taking the date value as a string convert the string with date format.

@rajarajan07: What I wanted to happen is that to enter a date (i.e. 2010-07-29 which is yyyy-mm-dd) in the textbox and that it will instantly be inserted in MySQL. Well is that ever possible, or I really do need to have separate textboxes for month day and year?

Thanks Experts.

Here's my current code found something on the internet about strtotime, and yes it's entring a data in my database but... but it's 01/01/1970 why is that?

<?php
$username="root";
$password="";
$database="matuto";

//$date=$_POST['Value1'];

$new_date = date("m/d/Y", strtotime($date));

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO datemonga VALUES
('','$new_date')";

mysql_query($query);

mysql_close();
?>

//$date=$_POST; --- Don't comment this line.Because you want to store this value.

$new_date = date("m/d/Y", strtotime($date)); In This line , What is the value of $date. Becoz,You are commenting Previoue line.

That'why , it is storing 01-01-1970(this is initial date value).

I'm trying to get my input in my textbox, the one on my html code

Value1: <input type="text" name="date">

Of course, inserting manually in my php code will work but I wanted to get the value that I inserted in my textbox and upon clicking SUBMIT it will store the date in my database

am i doing something wrong, thanks for the help

<html>
<head>
<title>..</title>
<body>
<form action="work1.php" method="post">
Value1: <input type="text" name="date"><br>
<input type="Submit"/>
</form>
</body>
</head>
</html>
<?php
$username="root";
$password="abcdef";
$database="test_date";
$date=$_POST["date"];  // Your First Problem Post value
echo $date;
echo "i m here";
$date = date("Y/m/d", strtotime($date));   // 2nd Problem Date Format
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO date VALUES('','$date')";

mysql_query($query);

mysql_close();
?>

It's Working .

Change according to this.

Thank You Sir Ankit_Parmar it's now working!!

ok, i had a question for you. Do you want to insert into database the current date...?

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.