Posts

Showing posts with the label datetime

Mastering MySQL Timestamps

Image
Timestamps in MySQL are used to represent date and time values, and they can store information with various levels of precision. MySQL provides several data types for working with timestamps, including DATE, TIME, DATETIME, TIMESTAMP, and YEAR. Let's focus on the two most commonly used timestamp data types in MySQL : DATETIME and TIMESTAMP . These types store both date and time information, but they have some differences in behavior. 1. DATETIME: The DATETIME data type stores date and time values in the format 'YYYY-MM-DD HH:MM:SS'. It does not automatically convert stored values to the server's time zone. The values are stored as provided. You can specify a time zone when inserting data into a DATETIME column, but the time zone information is not used for calculations. Example: '2023-09-03 14:30:00' 2. TIMESTAMP: The TIMESTAMP data type also stores date and time values in the format 'YYYY-MM-DD HH:MM:SS'. Unlike DATETIME, TIMESTAMP values...

Formatting MySQL Dates

Image
MySQL's DATE_FORMAT() function provides a wide range of format codes that you can use to represent date and time values in various formats.  It allows you to format a date or datetime value as a string using custom formatting. DATE_FORMAT(date, format) date: The date or datetime value you want to format. format: A string specifying the desired format for the output. This string can include special format codes that represent various components of the date and time (e.g., %Y for the year, %m for the month, %d for the day, %H for the hour, %i for the minute, %s for the second, etc.). SELECT DATE_FORMAT('2023-09-03', '%M %d, %Y'); // returns 'September 03, 2023'. Here is a list of commonly used format codes along with examples: %Y: Four-digit year (e.g., '2023'). %y: Two-digit year (e.g., '23' for 2023). %m: Month (01-12). %c: Month (1-12). %d: Day of the month (01-31). %e: Day of the month (1-31). %H: Hour (00-23). %h: Hour (01-12). %I: Hou...