php 如何从服务器获取日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6621572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get date and time from server
提问by user392406
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(服务器时间)中看到的内容不同。比我从代码中得到的。在 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.
为什么会发生这种情况以及我必须对代码进行哪些更改才能获得正确的服务器时间。
回答by Shef
You should set the timezoneto the one of the timezonesyou 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');
回答by Rohit Suthar
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";
?>
回答by Nasser Al-Wohaibi
Noneed to use date_default_timezone_set
for the whole script, just specify the timezone
you want with a DateTime object:
没有必要使用date_default_timezone_set
整个脚本,只需指定timezone
要与一个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)
回答by wonk0
You have to set the timezone, cf http://www.php.net/manual/en/book.datetime.php
回答by Nick Bristol
For enable PHP Extension intl , follow the Steps..
要启用 PHP Extension intl ,请按照步骤..
- Open the xampp/php/php.ini file in any editor.
- Search ";extension=php_intl.dll"
- kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to. extension=php_intl.dll.
- Save the xampp/php/php.ini file.
- Restart your xampp/wamp.
- 在任何编辑器中打开 xampp/php/php.ini 文件。
- 搜索“;extension=php_intl.dll”
- 请删除起始分号 (;),例如:;extension=php_intl.dll。到。扩展名=php_intl.dll。
- 保存 xampp/php/php.ini 文件。
- 重新启动您的 xampp/wamp。
回答by Sani Kamal
You should set the timezone to the one of the timezones you want. let set the Indian timezone
您应该将时区设置为您想要的时区之一。让我们设置印度时区
// set default timezone
date_default_timezone_set('Asia/Kolkata');
$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";
回答by Harold Flatz
You can use the "system( string $command[, int &$return_var] ) : string" function. For Windows system( "time" );, system( "time > output.txt" ); to put the response in the output.txt file. If you plan on deploying your website on someone else's server, then you may not want to hardcode the server time, as the server may move to an unknown timezone. Then it may be best to set the default time zone in the php.ini file. I use Hostinger and they allow you to configure that php.ini value.
您可以使用“system( string $command[, int &$return_var] ) : string”函数。对于 Windows system( "time" );, system( "time > output.txt" ); 将响应放入 output.txt 文件中。如果您计划在其他人的服务器上部署您的网站,那么您可能不想对服务器时间进行硬编码,因为服务器可能会移动到未知时区。那么最好在 php.ini 文件中设置默认时区。我使用 Hostinger,它们允许您配置该 php.ini 值。