What are the potential pitfalls of using date() and mktime() functions in PHP to manipulate dates retrieved from a database?
When using date() and mktime() functions in PHP to manipulate dates retrieved from a database, one potential pitfall is not accounting for time zone differences. To ensure accurate date manipulation, it is important to set the correct time zone before using these functions. This can be done by using the date_default_timezone_set() function with the appropriate time zone identifier.
// Retrieve date from database
$date_from_db = "2022-01-15";
// Set the time zone
date_default_timezone_set('America/New_York');
// Convert date to timestamp and manipulate it
$timestamp = strtotime($date_from_db);
$new_date = date('Y-m-d', strtotime('+1 week', $timestamp));
echo $new_date; // Output: 2022-01-22
Related questions
-
mssql_connect PHP Plesk
-
session_start session_register session variables
-
syntax errors class structure method structure