Jack Billy
Jack Billy

Reputation: 7211

How to get current time or date directly from internet?

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 timing even when there CMOS has failed or is out of battery.

Upvotes: 1

Views: 11042

Answers (3)

Kevin S
Kevin S

Reputation: 2753

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

Upvotes: 1

Stofke
Stofke

Reputation: 2958

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.

Upvotes: 1

corsiKa
corsiKa

Reputation: 82559

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

Upvotes: 0

Related Questions