{
    "slug": "mysql_datetime_types",
    "term": "MySQL Date and Time Types",
    "category": "database",
    "difficulty": "beginner",
    "short": "DATETIME, TIMESTAMP, DATE, TIME, and YEAR — each with different ranges, timezone handling, and storage sizes.",
    "long": "DATETIME stores date+time without timezone conversion (range: 1000-01-01 to 9999-12-31). TIMESTAMP stores as UTC and converts to the server timezone on retrieval (range: 1970-2038). TIMESTAMP auto-updates on row modification with DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. TIMESTAMP's 2038 problem is significant for long-lived data — use DATETIME for dates beyond 2038. Always store dates in UTC and convert for display in PHP.",
    "aliases": [
        "MySQL DATETIME vs TIMESTAMP",
        "MySQL date types",
        "TIMESTAMP Y2038",
        "MySQL time storage"
    ],
    "tags": [
        "mysql",
        "database",
        "datetime"
    ],
    "misconception": "TIMESTAMP handles timezones automatically so you don't need to think about them. TIMESTAMP converts using the MySQL server's timezone setting — if that changes, all stored timestamps display differently.",
    "why_it_matters": "TIMESTAMP columns silently hit Y2K38 — January 19, 2038, values wrap to 0. For data that will exist past 2038, DATETIME is required. Timezone confusion between DATETIME (no conversion) and TIMESTAMP (converted) causes display bugs.",
    "common_mistakes": [
        "Using TIMESTAMP for birth dates or historical records — wraps at 2038.",
        "Relying on MySQL timezone conversion instead of storing UTC explicitly.",
        "Comparing DATETIME columns across different timezone servers and getting wrong results."
    ],
    "when_to_use": [
        "Use DATETIME for all date/time storage — store values in UTC, convert to local timezone in application code.",
        "Use DATETIME ON UPDATE CURRENT_TIMESTAMP for updated_at audit columns."
    ],
    "avoid_when": [
        "Do not use TIMESTAMP for dates beyond 2038 — use DATETIME.",
        "Do not rely on MySQL's timezone conversion — set the application timezone explicitly and store UTC."
    ],
    "related": [
        "pdo",
        "mysql_charset_utf8mb4"
    ],
    "prerequisites": [
        "mysql_auto_increment"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/datetime.html"
    ],
    "bad_code": "-- TIMESTAMP wraps at 2038-01-19 — don't use for long-lived data\ncreated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Y2K38 problem",
    "good_code": "CREATE TABLE events (\n    id         INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n    title      VARCHAR(255) NOT NULL,\n    starts_at  DATETIME NOT NULL, -- store in UTC\n    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n) ENGINE=InnoDB;\n\n// PHP: store in UTC, convert for display\n$utc = new DateTimeImmutable('now', new DateTimeZone('UTC'));\n$stmt->execute([$utc->format('Y-m-d H:i:s')]);",
    "quick_fix": "Use DATETIME for flexibility and future-proofing, store values in UTC, handle timezone conversion in PHP",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_datetime_types",
        "html_url": "https://codeclaritylab.com/glossary/mysql_datetime_types",
        "json_url": "https://codeclaritylab.com/glossary/mysql_datetime_types.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[MySQL Date and Time Types](https://codeclaritylab.com/glossary/mysql_datetime_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_datetime_types"
            }
        }
    }
}