Reputation: 13
How can I set the document to expire every day at 2AM in PHP?
Upvotes: 1
Views: 2543
Reputation: 1553
// 2AM today
$epoch = mktime(2,0,0,date('n'),date('j'),date('y'));
// Go to tomorrow if current time > 2AM
$epoch += date('H') >= 2 ? 86400 : 0;
// Send header with RFC 2822 formatted date
header('Expires: '.date('r', $epoch));
Upvotes: 1
Reputation: 655309
You can use strtotime
to get the Unix timestamp at 2 AM and subtract that from the current time:
$diff = strtotime('2 AM') - time();
if ($diff < 0) $diff += 86400;
You can then use that difference for Cache-Control’s max-age:
header('Cache-Control: max-age='.$diff);
Upvotes: 0
Reputation: 131921
Set the Expires-HTTP-Header (for an example, see Wikipedia) with header(). You just need to specify the date.
header("Expires: " . date('D, d M Y') . " 02:00:00 GMT");
Note, that this is GMT. You maybe want to set another timezone.
Upvotes: 0
Reputation: 3302
header("Expires: " . date("D, j M Y", strtotime("now")) . " 02:00:00 GMT");
or
header("Expires: " . date("D, j M Y", strtotime("tomorrow")) . " 02:00:00 GMT");
Upvotes: 4
Reputation: 12078
You will want to use:
//assuming getTimeUnitl2AM() returns time in seconds until 2am
//if you need help implementing a function that returns
//time until 2am ask
$time = getTimeUntil2AM();
header("Expires: $time"); // set expiration time
Upvotes: 1