Wednesday, March 21, 2012

MySQL datatime type and timestamp



 When creating a time type column in MYSQL tables, there are 3 choices:
 DATE, DATETIME, and TIMESTAMP formats.
DATE: only date, format:'YYYY-MM-DD'
DATETIME, and TIMESTAMP: data and time, format 'YYYY-MM-DD HH:MM:SS'
There are 3 major MySQL time functions:
NOW(),CURDATE(), CURTIME()

NOW()            CURDATE() CURTIME()
2012-03-11   
11:45:34
    2012-03-11 11:45:34
Example:

Below is the example to insert current time in MySQL using MYSQL NOW() function.

Create theme table in cnsh_lms database
CREATE TABLE cnsh_lms.theme (
  `theme_id` mediumint(8) unsigned NOT NULL auto_increment,
   `revision` mediumint(8) unsigned DEFAULT 0,
  `main_width` varchar(255) NOT NULL,
   `page_width` varchar(255) NOT NULL,
    `author` varchar(255) DEFAULT NULL,  
   `submission_date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`theme_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Where  submission_date is the time format.

 INSERT into  cnsh_lms.theme VALUES(0, 1, '100%', '1100px', 'admin', NOW() );

NOW() function is the current time

SELECT * FROM cnsh_lms.theme t;

Return: 
2, 1, '100%', '1100px', 'admin', '2012-03-21 09:58:20'

2012-03-21 09:58:20 is the time I inserted using NOW() function
 
In PHP, we have similar function:
 
 $mysql_date = date('Y-m-d H:i:s');
 
 return something like:2012-03-21 10:19:24  
 

No comments:

Post a Comment