How can PHP be used to calculate the time difference between a start date and the current date?
To calculate the time difference between a start date and the current date in PHP, you can use the DateTime class to create DateTime objects for both dates and then calculate the difference using the diff() method. This method returns a DateInterval object which can then be used to extract the time difference in various formats such as days, hours, minutes, etc.
$start_date = new DateTime('2022-01-01');
$current_date = new DateTime();
$interval = $start_date->diff($current_date);
echo $interval->format('%a days, %h hours, %i minutes');
Keywords
Related Questions
- What are the recommended methods for handling and displaying SQL query results in PHP to prevent data leakage or manipulation?
- What potential issues may arise when using the mail() function in PHP for sending emails?
- How can the use of sprintf() function in PHP help in creating more organized and readable SQL queries?