在 PHP 网站中显示当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Current Time in PHP website
提问by Alai Angelo
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!
它已经在网站中显示当前日期和时间,但问题是我需要刷新页面才能看到时间和日期正在更新。请帮忙!
回答by Subhajit
Using PHP you can fetch the server time, please use this code to get the current time
使用 PHP 可以获取服务器时间,请使用此代码获取当前时间
<?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.
现在要更新当前时间和日期,您必须<div>使用显示日期的 (JQuery Ajax)刷新 element( ),我提供了一个链接,该链接描述了一种<element>定期刷新的方法。对您来说,它应该是 1 秒或 1000 毫秒。这是教程链接。
回答by James
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 setIntervaland update an element with the new time.
PHP 在服务器上运行,这意味着您必须刷新代码才能再次执行它。这里最好的解决方案是使用 JavaScript,因为您可以设置 asetInterval并使用新时间更新元素。
回答by Yash Kumar Verma
<?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;
?>
回答by Waruna Manjula
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
PHP 支持的时区列表:https: //www.w3schools.com/php/php_ref_timezones.asp
回答by Ruben Ruiz Perez
you need get date from php and print that inside a variable of javascript and make a loop for inside of navigator display hour
您需要从 php 获取日期并将其打印在 javascript 变量中,并在导航器显示小时内进行循环
check this code with javascript:
用javascript检查这段代码:
http://www.dynamicdrive.com/dynamicindex6/clock2.htm
http://www.dynamicdrive.com/dynamicindex6/clock2.htm
Or this code with jQuery:
或者这个代码与 jQuery:
http://www.jquery4u.com/snippets/create-jquery-digital-clock-jquery4u/
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.
您需要将 javascript 代码放在 php 页面的头部,然后打印实际日期,以便获取 javascript 代码的变量。

