2017-01-03 108 views
0

我尝试回显日期,但日期是一小时到早,我已经改变了ini的时区,但它不起作用。PHP时间/日期时区不工作

这就是我的代码:

$year = date('y'); 
$month = date('m'); 
$day = date('d'); 
$hour = date('H'); 
$min = date('i'); 
$sek = date('s'); 
$date = $day . "." . $month . "." . $year . "/" . $hour . ":" . $min . ":" . $sek; 
echo $date; 

提前感谢!

现在我有9:34,但该网站显示我8:34 重新启动后,它不工作!

+0

你应该改变ini文件后重新启动Apache的,[点击这里](http://stackoverflow.com/questions/12892331/do-i-need-to-restart-apache-after -changing-the-php-ini-file)了解更多详情。 – Tiger

+0

似乎是生成日期的特殊方式 - 为什么不喜欢'echo date('d.m.Y/H:i:s',strtotime('now'));'? – RamRaider

+0

正如我在我的答案中写的,你需要设置date_default_timezone_set(“CET”);或者你在什么时区。 – Michael

回答

1

此代码将让你的日期和不同大陆的调整。 代码炒菜锅

<?php 
date_default_timezone_set("Asia/Calcutta"); 
echo "The Date in Calcutta India is " . date("d-m-y"); 
echo "<br>The Time in Calcutta India is" . date("h-i-s-a"); 

date_default_timezone_set("America/New_York"); 
echo "<br><br>The Date in New York America is " . date("d-m-y"); 
echo "<br>The Time in New York America is " . date("h-i-s-a"); 
?> 
+0

我第一次这样做:date_default_timezone_set('UTC');它didesent工作 –

+0

您的欢迎... –

0

您可以设置时区:

// date_default_timezone_set('CET'); 
date_default_timezone_set('Continent/Country'); 
$year = date('y'); 
$month = date('m'); 
$day = date('d'); 
$hour = date('H'); 
$min = date('i'); 
$sek = date('s'); 
$date = $day . "." . $month . "." . $year . "/" . $hour . ":" . $min . ":" . $sek; 
echo $date; 
0

你需要设置默认的时区

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/jiangyahui/worksp/php/test_1.php on line 8 

03.01.17/08:21:18 
0

你确定你设置它的工作时区? 您可以回显phpinfo();并检查“date.timezone”字段。如果该值是不正确的,你可以在你的代码中使用date_default_timezone_set('YOUR TIMEZONE');变更时区

注意:如果你要打印日期时间在你的格式,你可以使用

date("y.m.d/H:i:s");