What are common pitfalls when using date() function in PHP and how can they be avoided?

One common pitfall when using the date() function in PHP is not specifying the correct timezone, which can lead to incorrect date and time outputs. To avoid this, always set the timezone using date_default_timezone_set() before using the date() function.

// Set the timezone to avoid incorrect date and time outputs
date_default_timezone_set('America/New_York');

// Get the current date and time
$currentDateTime = date('Y-m-d H:i:s');
echo $currentDateTime;