How can the date_default_timezone_set function be used effectively in PHP to avoid time offset issues?

When dealing with time-related functions in PHP, it is important to set the default timezone to avoid any unexpected time offset issues. This can be achieved by using the date_default_timezone_set function and specifying the desired timezone. By setting the timezone explicitly, you ensure that all date and time functions in your script will use the correct timezone, preventing any discrepancies in time calculations or formatting.

// Set the default timezone to 'America/New_York'
date_default_timezone_set('America/New_York');

// Now all date and time functions will use the 'America/New_York' timezone
echo date('Y-m-d H:i:s'); // Output: Current date and time in 'America/New_York'