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 limitations and considerations when using column aliases in the ORDER BY clause of SQL queries in PHP?
- How can the column structure of a CSV file be maintained when importing it into a MySQL database using PHP?
- What are the potential pitfalls of using preg_replace in PHP to remove certain tags from XML?