假设我有一个页面 abc.php
,其中包含以下 PHP 代码。
<?php
$current_time = date("Y-m-d H:i:s",time());
$curtim=strtotime($current_time);
echo $current_time.' '.$curtim;
?>
这个页面只是打印当前时间,比如 2015-07-27 14:18:18。当我以“不伤害的态度”工作时,我得到了正确的时间。
当我关闭选项卡并在一段时间后使用快捷键 Ctlr+Shift+T 重新打开它时,我仍然得到 2015-07-27 14:18:18 作为当前时间。 我猜该页面未使用该短键重新加载。
有没有办法在遇到这种情况时正确获取当前时间??
最佳答案
如果问题出在被缓存的页面上,应该修复一些 header 。
请参阅此处 Making sure a web page is not cached, across all browsers
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
关于php - 使用 date() 无法在 PHP 中获取正确的当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32324705/