Alai Angelo
Alai Angelo

Reputation: 61

Display Current Time in PHP website

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!

Upvotes: 3

Views: 77322

Answers (5)

Waruna Manjula
Waruna Manjula

Reputation: 3487

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

Upvotes: 0

Yash Kumar Verma
Yash Kumar Verma

Reputation: 9620

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

Upvotes: 2

Ruben Ruiz Perez
Ruben Ruiz Perez

Reputation: 1

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.

Upvotes: 0

Subhajit
Subhajit

Reputation: 1987

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.

Upvotes: 17

James
James

Reputation: 5169

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.

Upvotes: 3

Related Questions