如何从服务器获取日期和时间

时间:2020-12-04 16:30:27

I want to retrieve date and time from server and according to it do some thing. For this I used following code:

我想从服务器检索日期和时间,并根据它做一些事情。为此,我使用以下代码:

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

$current_date = "$date/$month/$year == $hour:$min:$sec";

This code returns proper date but problem is that what I see in my cpanel(server time) is diff. than what I get from code. In cpanel time is CDT while from code it is showing UTC which I reconfirm using following code

这段代码返回正确的日期,但问题是我在cpanel(服务器时间)中看到的是diff。而不是我从代码中得到的。在cpanel时间是CDT,而从代码它显示UTC,我使用以下代码再次确认

<?php echo date("r == e"); ?>

Why this is happening and what changes I have to do in my code so that I can get proper server time.

为什么会发生这种情况以及我在代码中需要做哪些更改才能获得适当的服务器时间。

5 个解决方案

#1


48  

You should set the timezone to the one of the timezones you want.

您应该将时区设置为所需的时区之一。

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

$current_date = "$date/$month/$year == $hour:$min:$sec";

Or a much shorter version:

或者更短的版本:

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$current_date = date('d/m/Y == H:i:s');

#2


7  

Try this -

尝试这个 -

<?php
date_default_timezone_set('Asia/Kolkata');

$timestamp = time();
$date_time = date("d-m-Y (D) H:i:s", $timestamp);
echo "Current date and local time on this server is $date_time";
?>

#3


3  

No need to use date_default_timezone_set for the whole script, just specify the timezone you want with a DateTime object:

无需对整个脚本使用date_default_timezone_set,只需使用DateTime对象指定所需的时区:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->format("Y-m-d\TH:i:sO"); // something like "2015-02-11T06:16:47+0100" (ISO 8601)

#4


1  

You have to set the timezone, cf http://www.php.net/manual/en/book.datetime.php

你必须设置时区,参见http://www.php.net/manual/en/book.datetime.php

#5


0  

For enable PHP Extension intl , follow the Steps..

要启用PHP扩展intl,请按照步骤..

  1. Open the xampp/php/php.ini file in any editor.
  2. 在任何编辑器中打开xampp / php / php.ini文件。
  3. Search ";extension=php_intl.dll"
  4. 搜索“; extension = php_intl.dll”
  5. kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to. extension=php_intl.dll.
  6. 请删除起始分号(;),如:; extension = php_intl.dll。至。延长= php_intl.dll。
  7. Save the xampp/php/php.ini file.
  8. 保存xampp / php / php.ini文件。
  9. Restart your xampp/wamp.
  10. 重新启动你的xampp / wamp。

#1


48  

You should set the timezone to the one of the timezones you want.

您应该将时区设置为所需的时区之一。

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

$current_date = "$date/$month/$year == $hour:$min:$sec";

Or a much shorter version:

或者更短的版本:

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$current_date = date('d/m/Y == H:i:s');

#2


7  

Try this -

尝试这个 -

<?php
date_default_timezone_set('Asia/Kolkata');

$timestamp = time();
$date_time = date("d-m-Y (D) H:i:s", $timestamp);
echo "Current date and local time on this server is $date_time";
?>

#3


3  

No need to use date_default_timezone_set for the whole script, just specify the timezone you want with a DateTime object:

无需对整个脚本使用date_default_timezone_set,只需使用DateTime对象指定所需的时区:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->format("Y-m-d\TH:i:sO"); // something like "2015-02-11T06:16:47+0100" (ISO 8601)

#4


1  

You have to set the timezone, cf http://www.php.net/manual/en/book.datetime.php

你必须设置时区,参见http://www.php.net/manual/en/book.datetime.php

#5


0  

For enable PHP Extension intl , follow the Steps..

要启用PHP扩展intl,请按照步骤..

  1. Open the xampp/php/php.ini file in any editor.
  2. 在任何编辑器中打开xampp / php / php.ini文件。
  3. Search ";extension=php_intl.dll"
  4. 搜索“; extension = php_intl.dll”
  5. kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to. extension=php_intl.dll.
  6. 请删除起始分号(;),如:; extension = php_intl.dll。至。延长= php_intl.dll。
  7. Save the xampp/php/php.ini file.
  8. 保存xampp / php / php.ini文件。
  9. Restart your xampp/wamp.
  10. 重新启动你的xampp / wamp。