如何从服务器获取日期和时间 [英] How to get date and time from server

查看:171
本文介绍了如何从服务器获取日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

<?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.

推荐答案

你应该将时区设置为 timezones 你想要的。

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

或更短的版本:

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

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

这篇关于如何从服务器获取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆