How to get server time not localtime in php

大兔子大兔子 提交于 2020-01-15 07:26:08

问题


First Part of the Question :

When I m using new Date (); in Javascript.

it gives

Fri Jun 19 2015 11:18:02 GMT+0530 (India Standard Time)

This is giving my local machine time.whereas server time is 7 mins ahead.

Is there any function which gives server time not local machine time same exactly as new Date() But from server not localMachine.

In My project i am using codeigniter.is there any method to keep datetime same as servertime not localmachine time.

so when i use echo date('Y-m-d H:i:s'). This gives server time.

Second part of the question :

How to echo date in php in the same format like new Date() in Javascript:

new Date() Prints Fri Jun 19 2015 11:18:02 GMT+0530 (India Standard Time)

But in Php date('D M Y G:i:s T', time()); is giving Fri Jun 19 2015 11:18:02 IST

How to achieve same format like new Date().


回答1:


If you work on real server I believe you can get proper time by this snippet.

function server_time()
{
  $this->load->helper('date');
  echo now();  
  echo "<br>";
}

If you use xampp or wamp on your local pc that time you'll get your local pc time.




回答2:


Create an ajax script which will retrieve server time

function server_time()
{
    $this->load->helper('date');
    echo now();  // UNIX timestamp
}

Then convert it in desired format with javascript

var date = new Date(response*1000);


来源:https://stackoverflow.com/questions/30930848/how-to-get-server-time-not-localtime-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!