Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting correct current time in PHP using date()

Tags:

php

Let's say I have a page abc.php where the following PHP code lies.

<?php
   $current_time = date("Y-m-d H:i:s",time());
   $curtim=strtotime($current_time);
   echo $current_time.' '.$curtim;
?>

This page simply prints the current time like 2015-07-27 14:18:18. And I get the correct time when I am working with a 'Do No Harm Attitude'.

When I close the tab and reopen it using a shortcut key- Ctlr+Shift+T after some time, i still get 2015-07-27 14:18:18 as current time. I guess the page is not getting reloaded using that short key.

Is there a way to fetch the current time correctly when such scenarios come??

like image 902
Sajeev C Avatar asked Aug 20 '26 03:08

Sajeev C


1 Answers

If the problem is the page being cached some header should fix.

See here 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.
like image 118
Alex Avatar answered Aug 21 '26 17:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!