How can DateTime::add() be used to add only 1 hour accurately?
When using DateTime::add() to add 1 hour, we need to be careful as adding a fixed number of hours may not always be accurate due to daylight saving time changes or leap years. To accurately add 1 hour, we can use the DateInterval class to specify the exact interval of 1 hour. This ensures that the addition is precise and takes into account any time changes that may affect the result.
$date = new DateTime();
$date->add(new DateInterval('PT1H'));
echo $date->format('Y-m-d H:i:s');
Keywords
Related Questions
- What are some best practices for debugging PHP scripts that are initiated through AJAX requests?
- What are some best practices for handling user input validation and error handling in PHP scripts that involve variable manipulation?
- What tools and techniques can be used to troubleshoot issues with linking between local and live PHP sites?