简体   繁体   English

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

[英]How to get date and time from server

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.此代码返回正确的日期,但问题是我在 cpanel(服务器时间)中看到的是差异。 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 时间是 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.为什么会发生这种情况以及我必须对代码进行哪些更改才能获得适当的服务器时间。

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');

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";
?>

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 object 指定您想要的timezone

$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)

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

For enable PHP Extension intl, follow the Steps..要启用 PHP Extension intl,请按照以下步骤操作。

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

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";

You can use the "system( string $command[, int &$return_var] ): string" function.您可以使用“系统(字符串 $command[, int &$return_var]):字符串”function。 For Windows system( "time" );, system( "time > output.txt" );对于 Windows 系统(“时间”);,系统(“时间 > output.txt”); to put the response in the output.txt file.将响应放入 output.txt 文件中。 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.那么最好在 php.ini 文件中设置默认时区。 I use Hostinger and they allow you to configure that php.ini value.我使用 Hostinger,它们允许您配置 php.ini 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM