3

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??

Sajeev C
  • 1,458
  • 4
  • 16
  • 27
  • 1
    you are openening this page 'as it was' with that shortcut. You can use javascript to display the time, or an AJAX call, to get the time from the server on pageload – Alexis Peters Sep 01 '15 at 06:20
  • i think because the page is cached, if you refresh the page the time should be changed? – Andrew Sep 01 '15 at 06:24
  • 1
    @Andrew I know refreshing the page would fetch me the correct time but we can't rely on users. They may use any method to get to a page. That's why I raised this question. – Sajeev C Sep 01 '15 at 06:28
  • I don't think this question is PHP specific... – Amarnasan Sep 01 '15 at 06:30
  • @SajeevC then this should be a front end question....php is dead once it is outputed.. – Andrew Sep 01 '15 at 06:36
  • @Andrew in that case you should use Ajax to update the page with current server date time. once the page is loaded the php alone wouldn't be able to do anything to update the time.use js to update user with his/her system time or Ajax php to update user with the server date time on time :) – aimme Sep 01 '15 at 06:43
  • The response header (with eventual cache options) are sent the first time you load the page, those options are (should) be used by the browser to choose how to refresh the content, so, php is dead but left some will. – Alex Sep 01 '15 at 06:47
  • 1
    @aimme yea, I assumed it is a php question because I look upon the tag... if datetime is a concern then its best to store in database with set to utc time and convert back to user when requested..at least this is what I used.. – Andrew Sep 01 '15 at 06:51
  • So you should go for @Alireza Mirzaeiyan's solution. As Ajax is used for dynamic content updating using server contents :) – aimme Sep 01 '15 at 06:56

1 Answers1

3

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.
Community
  • 1
  • 1
Alex
  • 3,116
  • 1
  • 19
  • 37