{
    "slug": "php_date_timezone",
    "term": "PHP Date/Timezone Pitfalls",
    "category": "php",
    "difficulty": "intermediate",
    "short": "Common PHP date and timezone bugs — relying on date_default_timezone_set(), comparing DateTime objects across timezones, and strtotime() ambiguity.",
    "long": "PHP date pitfalls: date_default_timezone_set() is process-wide — changing it affects all date operations globally. DateTime is mutable (use DateTimeImmutable). strtotime() is locale-dependent and silently returns false for invalid strings. Comparing DateTime across timezones: two DateTime objects representing the same instant but different timezones compare as unequal by value. Carbon wraps DateTimeImmutable with testing support (Carbon::setTestNow()). Always: store UTC, display in user's timezone, use DateTimeImmutable, validate date strings explicitly.",
    "aliases": [
        "PHP timezone",
        "DateTime timezone",
        "Carbon",
        "date_default_timezone_set"
    ],
    "tags": [
        "php",
        "dates",
        "bugs"
    ],
    "misconception": "date_default_timezone_set() is safe to call in library code — calling it in a library changes the timezone for the entire application, breaking any code that relies on the previous timezone setting.",
    "why_it_matters": "A PHP application without explicit timezone handling shows different times to users in different regions, stores ambiguous timestamps that cannot be reliably converted, and breaks during DST transitions.",
    "common_mistakes": [
        "Calling date_default_timezone_set() in library code — it is a global setting; libraries must never change it.",
        "DateTime instead of DateTimeImmutable — mutation surprises when DateTime is passed to functions.",
        "Comparing DateTime objects with == — compares string representation not the same instant.",
        "strtotime() without checking for false — invalid strings return false which converts to timestamp 0 (1970)."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "legacy_date_functions",
        "timezone_handling",
        "date_functions"
    ],
    "prerequisites": [
        "date_functions",
        "timezone_handling",
        "legacy_date_functions"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.datetimeimmutable.php"
    ],
    "bad_code": "// Mutable DateTime — surprise mutation:\n$start = new DateTime('2026-01-01');\n$end   = $start->modify('+1 month'); // $start is also modified!\necho $start->format('Y-m-d'); // 2026-02-01 — not 2026-01-01!\n\n// Timezone comparison bug:\n$utc = new DateTime('12:00', new DateTimeZone('UTC'));\n$bst = new DateTime('13:00', new DateTimeZone('Europe/London')); // Same instant\nvar_dump($utc == $bst); // false — same instant, different representation",
    "good_code": "// DateTimeImmutable — safe:\n$start = new DateTimeImmutable('2026-01-01');\n$end   = $start->modify('+1 month'); // $start unchanged\necho $start->format('Y-m-d'); // 2026-01-01 — correct\n\n// Correct comparison — convert to same timezone or compare timestamps:\n$utc = new DateTimeImmutable('12:00', new DateTimeZone('UTC'));\n$bst = new DateTimeImmutable('13:00', new DateTimeZone('Europe/London'));\nvar_dump($utc->getTimestamp() === $bst->getTimestamp()); // true\n\n// Validate before using strtotime:\n$ts = strtotime($userInput);\nif ($ts === false) throw new InvalidArgumentException('Invalid date');",
    "quick_fix": "Set date.timezone = UTC in php.ini and always pass explicit DateTimeZone objects — relying on the system timezone causes bugs when servers are in different regions",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_date_timezone",
        "html_url": "https://codeclaritylab.com/glossary/php_date_timezone",
        "json_url": "https://codeclaritylab.com/glossary/php_date_timezone.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": "[PHP Date/Timezone Pitfalls](https://codeclaritylab.com/glossary/php_date_timezone) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_date_timezone"
            }
        }
    }
}