how to insert date in mysql using php?

Rate this post

Today, We want to share with you how to insert date in mysql using php.In this post we will show you PHP mysql insert date format, hear for How to Insert a Date in MySQL we will give you demo and example for implement.In this post, we will learn about How To Insert Date In Mysql Using Php? with an example.

How to Insert Date in a MySQL database table?

For Insert date Into MySQL in ‘dd-mm-yyyy’ format Using PHP first of all I have to make a simple table in data base like bellow example for InsertDate current date in mysql using php.

how to Insert a Date in MySQL using CURDATE

$query_auto = "INSERT INTO members (member_name, join_dt) VALUE ('DATE: Auto CURDATE()', CURDATE() )";

how to Insert a Date in MySQL manually?

$query_manual = "INSERT INTO members (member_name, join_dt) VALUES ('DATE: Manual Date', '2021-7-04')";

how to Insert a YEAR in MySQL using CURDATE?

INSERT INTO members (member_name, join_dt) VALUE ('YEAR: Auto CURDATE()', CURDATE() )";

how to use DATETIME?

INSERT INTO members (member_name, join_dt) VALUE ('DATETIME: Auto CURDATE()', CURDATE() )";

PHP script which sets a date in MySQL manually

<?php
$query_date = "INSERT INTO members (member_name, join_dt) VALUES ('DATE: Manual Date', '2021-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>

PHP script which sets a date in MySQL manually

<?php
$query_date = "INSERT INTO members (member_name, join_dt) VALUE ('DATE: Auto CURDATE()', CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>

Inserting present date and time to a field of mysql table

Example: 2005-12-26 23:50:30

INSERT INTO members (dt,dt2) VALUES ('2004-05-05 23:56:25', '2005-06-12');

Free Live Chat for Any Issue

PHP Script to add date and time to MySQL table

<?Php
require "config.php"; // Database Connection 
$dt1=date("Y-m-d");
$dt2=date("Y-m-d H:i:s");

$sql="INSERT INTO members(dt,dt2) VALUES ('$dt2', '$dt1')";
$sql=$dbo->prepare($sql);
if($sql->execute()){
echo " Date stored in table  ";
}
else{
echo " Not able to add record  ";
print_r($sql->errorInfo()); 
}

?>

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `dt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `dt2` date NOT NULL DEFAULT '0000-00-00',
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `id_2` (`id`),
  UNIQUE KEY `id_3` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

Download

I hope you get an idea about how to insert date in mysql using php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.