-3

How to get real date from php.

I am using Date date(); This function returns system date.

I want to get the real date with irrespective of the users computers date.

Do we need any api to access the date or do we have any function that grab's the server date and time?

  • 1
    `date()` function returns system date of server .. and what do you mean by real date ?? – M A SIDDIQUI Jan 13 '17 at 17:34
  • 1
    what do you mean with "real"? also you can't get the user Date with a server-side(PHP) language but you need a client-side (JavaScript) – paolobasso Jan 13 '17 at 17:35
  • @MASIDDIQUI When I change the system date server captures my system current fake date. – user7416096 Jan 13 '17 at 17:35
  • Possible duplicate of [How can I get the user's local time instead of the server's time?](http://stackoverflow.com/questions/2705067/how-can-i-get-the-users-local-time-instead-of-the-servers-time) – paolobasso Jan 13 '17 at 17:35
  • @paolo.basso99 I want get server date not system date. – user7416096 Jan 13 '17 at 17:36
  • What is a *real date* and how does it differ from a non *real date*? What, in your words, is the difference between the server and the system? – HPierce Jan 13 '17 at 17:39
  • Your question is confusing. There is no "fake" date. PHP is a server side language, and it's date functions would rely on your server's clock. Server and system would also be the same in this case – georaldc Jan 13 '17 at 17:41
  • @georaldc I changed my system date but when I executed from my xampp php server it gives system date. Not real date. – user7416096 Jan 13 '17 at 17:43
  • @georaldc Oh may be localhost runs on my computer so does my system act as server? Do I get real date if I put it on my hosting server? – user7416096 Jan 13 '17 at 17:44
  • What date were you expecting then if the system date wasn't what you wanted? – georaldc Jan 13 '17 at 17:45
  • 1
    Yes, your local machine would be your server in that case. It's been a while since I last used xampp, but I believe PHP would in this case be using your computer's clock as reference. – georaldc Jan 13 '17 at 17:46

3 Answers3

1

Your question seems to demonstrate a bit of confusion about where date values come from. I will try to clarify for you:

PHP code runs on your web server, and so when you call date(), the DateTime returns the current date on the server. (You added a comment after I wrote this that clarified that this seems to be the source of your confusion: if you're running your application in XAMPP then your local machine is your server, and so changing your clock time would change the value returned by date())

If you want to know the date on the client (the user interacting with your system instead), and make that date available to your PHP code, you would need to have the client submit it, for example using AJAX.

If you want to know the date as defined by some official authority, independent of the system settings of your server or your user, you could send a request to NIST at http://nist.time.gov/actualtime.cgi. Rather than doing that in your PHP code, though, I would suggest simply setting your server time to be accurate and using date().

AmericanUmlaut
  • 2,817
  • 2
  • 17
  • 27
0

date function returns server date not visitors' local date

For example:

<?php
echo date('Y-m-d H:i:s');

http://ideone.com/cZ5wq3

aperpen
  • 718
  • 7
  • 10
0

You can get server date based on time zone as per your requirement like:

date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());  //returns date and time at Australia/Melbourne

Similarly for Asia/Kolkata:

date_default_timezone_set('Asia/Kolkata');
echo $date = date('m/d/Y h:i:s a', time());

Get all time zones here : http://php.net/manual/en/timezones.asia.php

Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30