how to insert date in mysql using php?

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.

Read Also:  Laravel 5.8 Set file folder storage permissions

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');

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 ;

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.

Read Also:  Laravel 6 Advanced Where Clauses Tutorial With Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Config Class to store configuration data
  2. Read Also:  PHP string to int | php cast to int
  3. Read Also:  PHP Download Remote File from URL using CURL
  4. Read Also:  How to get last record of database table in Laravel?
  5. Read Also:  Laravel 6 Delete and soft delete Examples
  6. Read Also:  Laravel 6 Eloquent LEFT JOIN Query Example Tutorial
  7. Read Also:  PHP Google Multi factor authentication Tutorial With Example
  8. Read Also:  Laravel Collection Contains Example
  9. Read Also:  PHP Google Multi factor authentication Tutorial With Example
  10. Read Also:  PHP Config Class to store configuration data
CLOSEX