mysql dateformat code example

Example 1: mysql format date

DATE_FORMAT(date, format)
-- E.g.
SELECT DATE_FORMAT(dateField, '%m/%d/%Y') FROM TableName;
-- See https://www.mysqltutorial.org/mysql-date_format/ for available formats

Example 2: format time mysql

-- use DATE_FORMAT with %H %i
-- SELECT DATE_FORMAT(MemberBookFacility.time, '%H:%i')
"45": "09:00",
"24": "10:00",
"42": "11:00",
"48": "12:00",

-- ONcakephp must use below format
$this->virtualFields['time'] = "DATE_FORMAT(MemberBookFacility.time, '%H:%i')";	// using this for use concat
	
		return $this->find('list', array(
          'conditions' => $conditions,
          'fields' => array(
            'MemberBookFacility.id', 
            'time',
          ),
          'order' => array(
            'MemberBookFacility.time ASC',
          ),
        ));

Example 3: mysql format des dates

DATE : stocke une date au format AAAA-MM-JJ (Année-Mois-Jour) ;

TIME : stocke un moment au format HH:MM:SS (Heures:Minutes:Secondes) ;

DATETIME : stocke la combinaison d'une date et d'un moment de la journée au format AAAA-MM-JJ HH:MM:SS. Ce type de champ est donc plus précis ;

TIMESTAMP : stocke le nombre de secondes passées depuis le 1er janvier 1970 à 00 h 00 min 00 s ;

YEAR : stocke une année, soit au format AA, soit au format AAAA.

Example 4: get individual date elements in mysql

-- To get the year of a date in mysql
SELECT YEAR(NOW());

-- To get the month of a date in mysql
SELECT MONTH(NOW());

-- To get the day of a date in mysql
SELECT DAY(NOW());

-- To get the hour of a date in mysql
SELECT HOUR(NOW());

-- To get the minute of a date in mysql
SELECT MINUTE(NOW());

-- To get the second of a date in mysql
SELECT SECOND(NOW());

Tags:

Sql Example