YYYY-mm-dd
Example: 2005-12-26 YYYY-mm-dd H:i:s
Example: 2005-12-26 23:50:30INSERT INTO dt_tb (dt,dt2) VALUES ('2004-05-05 23:56:25', '2005-06-12');
PHP date function to changing date formats from one type to other
$dt1=date("Y-m-d");
$dt2=date("Y-m-d H:i:s");
Now our sql query with these two date values will be like this
INSERT INTO dt_tb(dt,dt2) VALUES ('$dt2', '$dt1');
<?Php
require "config.php"; // Database Connection
$dt1=date("Y-m-d");
$dt2=date("Y-m-d H:i:s");
$sql="INSERT INTO dt_tb(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());
}
?>
To create your table here is the sql dump.
CREATE TABLE IF NOT EXISTS `dt_tb` (
`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 ;
str_to_date() function can be used to converting string data to date & time format to insert in MySQL tablePuneet Verma | 31-12-2009 |
Is there a way to provide condition check while using INSERT in sql querry |
John | 03-02-2010 |
Sorry, but I can't get it to work. Why don't you include this as part of a practical database example. I've been all over the net just to stick a date field in my database. Nothing works - it can't be that hard to do a practial working example of this. |
Matt McCarty | 25-04-2010 |
@John... I have a variable list (one is a date to insert, i.e. $dtCreated=date('Y-m-d'); Then I have my INSERT statement with that variable (using PHP) $query = "INSERT INTO myTable VALUES ('$dtCreated')"; If you have more than one variable, order matters! |
chan | 20-08-2010 |
I need aprocedure which generates a table with columns week,start_dt,End_dt. Week should have all 52 weeks,start_dt should every weeks start day ,end_dt should have everyweeks end date. Please help Thanks |
madhavi | 24-04-2014 |
I need to alter table with current date and time in mysql |
smo | 14-11-2014 |
One practical example using PHP is added to this tutorial. |
sadia | 24-08-2015 |
Very helpful |
deepa | 30-05-2016 |
insted of php code i want html or javascript code..can u give us> |
Shital Gamaji Patil | 05-01-2017 |
how to add from and to date in a single column. or in single column how to add two dates. |
smo1234 | 19-02-2017 |
You can store dates as string in a single column. You can use any delimiter to separate the dates, while retrieving you can use the same delimiter to separate them. But what is the problem in storing them in two different columns ? |