How can PHP be used to compare the modification dates of images and thumbnails to determine if a new thumbnail needs to be generated or if the existing one can be used?

To compare the modification dates of images and thumbnails in PHP, you can use the filemtime() function to get the last modification timestamp of the image and thumbnail files. By comparing these timestamps, you can determine if a new thumbnail needs to be generated or if the existing one can be used.

$image_path = 'path/to/image.jpg';
$thumbnail_path = 'path/to/thumbnail.jpg';

$image_modified_time = filemtime($image_path);
$thumbnail_modified_time = filemtime($thumbnail_path);

if ($thumbnail_modified_time < $image_modified_time) {
    // Generate new thumbnail
    // Your code to generate a new thumbnail goes here
} else {
    // Use existing thumbnail
    // Your code to use the existing thumbnail goes here
}