How can PHP be used to format timestamps and durations for displaying time-related information in a web application?
When displaying time-related information in a web application, it is important to format timestamps and durations in a user-friendly way. PHP provides functions like date() and strtotime() that can be used to format timestamps and calculate durations. To display timestamps in a specific format, use the date() function with the desired format string. For durations, calculate the time interval using strtotime() and then format it accordingly.
// Format timestamp for display
$timestamp = time(); // current timestamp
$formatted_timestamp = date('Y-m-d H:i:s', $timestamp);
echo $formatted_timestamp;
// Calculate and format duration
$start_time = strtotime('2022-01-01 12:00:00');
$end_time = time(); // current timestamp
$duration = $end_time - $start_time;
$hours = floor($duration / 3600);
$minutes = floor(($duration % 3600) / 60);
$seconds = $duration % 60;
$formatted_duration = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
echo $formatted_duration;