3

So, I have this code:

  print date("l, F j, Y h:m:s a", strtotime("+8 hours"));

and it is already displaying the current date and time in the website, but the problem is that I need to refresh the page in order to see that the time and date is updating. Please help!

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
Alai Angelo
  • 61
  • 1
  • 3
  • 6
  • 1
    You should use Javascript for that. – h00ligan May 25 '12 at 08:16
  • 2
    use ajax to call php script each second and display updated time on webpage. or, simpler, use javascript to display current time. – maialithar May 25 '12 at 08:16
  • @maialithar lol @ calling a php script with AJAX every second to update the time. See this answer for how to display the user's system time. You could adapt that script so that it first calculates the difference between user's time and your time. Then use that difference to calculate "your time" on the user's page every second, using the initially calculated difference. I don't know if that's the best way, but probably better than once-a-second ajax calls. Imagine if you had millions of users! Your system would crash! http://stackoverflow.com/a/6787593/631764 – Buttle Butkus Jul 06 '15 at 06:48
  • Add your time zone. date_default_timezone_set("Asia/Colombo"); ... echo date("Y-m-d H:i:s"); // or any format ... PHP Supported Timezone List : https://www.w3schools.com/php/php_ref_timezones.asp – Waruna Manjula Apr 25 '19 at 09:55

5 Answers5

17

Using PHP you can fetch the server time, please use this code to get the current time

<?php echo date("D M d, Y G:i a"); ?>

You can change the Date format as per your requirement, please look here.

Now to update the current time and date you have to refresh the element(<div>) using (JQuery Ajax) in which the date is displayed, I have provided a link which describes a way to refresh <element> in regular interval. For you it should be 1sec or 1000 miliseconds. Here is the Tutorial link.

Subhajit
  • 1,929
  • 3
  • 19
  • 21
  • i tried the tutorial and it works, but the date and time appears like this 1337937556 – Alai Angelo May 25 '12 at 09:19
  • In the PHP file please use . If I asume that the PHP page name is date.php. Now call this php page in the script in this section:- j.ajax({ url: "date.php", – Subhajit May 25 '12 at 09:49
  • I already found a script and the time worked! But there is a problem on the date. It shows "April NaN 2012" instead of "May 25 2012" – Alai Angelo May 25 '12 at 10:25
3

PHP runs on the server, which means that you have to refresh for the code to execute it again. The best solution here is to use JavaScript as you could set a setInterval and update an element with the new time.

James
  • 5,137
  • 5
  • 40
  • 80
2
<?php
//$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
//$today = date("m.d.y");                         // 03.10.01
//$today = date("j, n, Y");                       // 10, 3, 2001
//$today = date("Ymd");                           // 20010310
//$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
//$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
//$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
//$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
//$today = date("H:i:s");                         // 17:16:18

echo $today;
?>
Yash Kumar Verma
  • 9,427
  • 2
  • 17
  • 28
0

you need get date from php and print that inside a variable of javascript and make a loop for inside of navigator display hour

check this code with javascript:

http://www.dynamicdrive.com/dynamicindex6/clock2.htm

Or this code with jQuery:

http://www.jquery4u.com/snippets/create-jquery-digital-clock-jquery4u/

you need put javascrip code in head of php page and then print actual date, for get in variables for javascript code.

0

Add your time zone

date_default_timezone_set("Asia/Colombo");

echo date("Y-m-d H:i:s");  // or any format

PHP Supported Timezone List : https://www.w3schools.com/php/php_ref_timezones.asp

Waruna Manjula
  • 3,067
  • 1
  • 34
  • 33