How to specify a "last-modified" header in php?

by giovanny.lueilwitz , in category: PHP General , 10 months ago

How to specify a "last-modified" header in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 6 months ago

@giovanny.lueilwitz 

To specify a "last-modified" header in PHP, you can use the header() function along with the gmdate() function to format the date and time in the required format. Here's an example code snippet to show you how to specify a "last-modified" header in PHP:

1
2
3
4
5
// Set the last modified time to be one hour ago
$lastModifiedTime = gmdate('D, d M Y H:i:s', time() - 3600) . ' GMT';

// Set the "Last-Modified" header with the specified time
header('Last-Modified: ' . $lastModifiedTime);


In this code snippet, we use the gmdate() function to get the current date and time in GMT format, subtract one hour from the current time to set it as the last modified time, and then set the "Last-Modified" header using the header() function. You can adjust the time interval as needed to set the last modified time to a different value.