开发者

How to get current time or date directly from internet?

开发者 https://www.devze.com 2023-02-20 18:22 出处:网络
My computer have lost the power of CMOS battery! And my computer is showing current year as 2004 and time as 12:00 AM! And one of my codes are not working well like --

My computer have lost the power of CMOS battery! And my computer is showing current year as 2004 and time as 12:00 AM! And one of my codes are not working well like --

<?php
  echo date("Y");
?>

Which provides me the output as ---

2004

But I know that the current year is 2011. Also in Facebook and Stack overflow and other sites the time is correct! How is this possible? Is there any other code for displaying the current date and time? Can anyone suggest me a code that could provide my website users correct timi开发者_开发技巧ng even when there CMOS has failed or is out of battery.


Are you running Linux (or some variant)? I found this page in a quick google search.

NTP Time Synchronization Script

The problem like Stofke said is that PHP just grabs the time from your computer because it doesn't know about NTP or stuff like that. So what you really need is a script that makes sure your server has accurate time and then PHP will grab the accurate time from your server.

EDIT: Also just as a resource, this link explains how to sync up to ntp via the command line and has a little excerpt about Windows at the bottom too. NTP HOWTO


PHP is run on the server so it takes the server time.

Right now I assume you develop on your local computer so it acts as the server and displays it wrong but once you upload your scripts to an online server it will use the time/date of that server and if that one is set correctly your users will see 2011.


All you need: http://www.whattimeisit.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消