setModifiedDate


This PHP function can be used to determine whether a page should return content or a 304 Not Modified header. To use it simply call it with the date that your content (from a database for example) was last modified. If this date is earlier than what the browser is looking for, a 304 is returned. The date can be in any English readable format that can be parsed by strtotime() in your locale.

/**
 * Checks if the page needs to be resent.
 *
 * Determines whether a 304 Not Changed
 * header can be returned to the browser
 * which eases up on bandwidth usage.
 * Good for RSS feeds and large forum
 * pages. The browser must send the
 * If-Modified-Since header for this to work.
 *
 * @return void
 * @param mixed $content_date
 * The date of the content of the page to compare
 * with the header from the browser.
 *
 */
function setModifiedDate($content_date) {
    $req_headers = apache_request_headers();
    if (isset($req_headers['If-Modified-Since'])) {
        $cd = strtotime($content_date);
        $ims = strtotime($req_headers['If-Modified-Since']);
        if ($cd <= $ims) {
            $retdate = gmdate("D, d M Y H:i:s") . ' GMT';
            header("HTTP/1.1 304 Not Changed");
            header("Date: $retdate");
            exit();
        }
    }
}
Written by Colin Bate