What can cause the issue of getting the date output as 01.01.1970 in PHP when the database contains dates like 2005-02-04 and 2005-03-21?
The issue of getting the date output as 01.01.1970 in PHP can be caused by incorrect formatting of the date string when retrieving it from the database. To solve this issue, you can use the PHP `strtotime()` function to convert the date string into a Unix timestamp before formatting it using the `date()` function.
// Retrieve date from the database
$date_from_db = "2005-02-04";
// Convert date string to Unix timestamp
$timestamp = strtotime($date_from_db);
// Format the timestamp as desired (e.g. "Y-m-d")
$formatted_date = date("Y-m-d", $timestamp);
echo $formatted_date; // Output: 2005-02-04
Keywords
Related Questions
- What are the potential solutions to avoid the browser error message when updating a PHP page?
- How can multiple values be passed through a URL in PHP using the GET method, and what considerations should be made when implementing this?
- How can PHP developers ensure the scalability of their code when creating browser games with dynamic features?