更改今天的日期和时间在php [英] Change today's date and time in php
问题描述
是否可以更改php当前的日期和时间(像set_time(strtotime('1999-09-09'))),所以当我打电话time()它将返回1999-09-09的时间戳?我需要将其设置为整个应用程序。
Is it possible to change php's current date and time (something like set_time(strtotime('1999-09-09')) so when i call time() it will return me timestamp for 1999-09-09? I need to set it globally for whole app.
编辑
我已经有一个旧的应用程序,我需要设置当前的日期
我不能接受任何自定义的功能想象一下,我应该检查所有的代码,并替换每次()调用即使我会这样做它不会帮助,因为有一些函数,如我需要替换的getdate()。
I already have one old app and i need to make possible to set up current date. I can't accept any custom functions. Imagine i should review all the code and replace every time() call. Even if i'll do so then it will not help because there are functions like getdate() which i need to replace as well.
推荐答案
time()
函数实际上会询问系统时钟,所以你必须更新服务器系统时钟的时间。需要记住的几点:
The time()
functions actually asks the system clock for the time, so you'd have to update the time of the server's system clock. A few things to keep in mind:
- 在一个Unix服务器上这样做与Windows服务器不同,如果您希望代码与两者兼容,则必须小心
- 有时候特别是在共享托管情况下)您不能更改系统时间
- 在脚本执行后,系统时间的更改将保留,除非您稍后更改。因此,您有风险真的拧紧了系统时钟。
如果要更改整个应用程序的全球时间,我建议您静态变量 $ timeOffset
并创建自己的 time()
函数如下:
If you want to change the time globally for an entire app, I recommend having a static variable $timeOffset
and create your own time()
function as follows:
function myTime(){
global $timeOffset;
return time()+$timeOffset;
}
如果您将所有这一切纳入考量,仍然希望更改系统时间,但是,您需要使用 shell_exec()
If you've taken all of that into account and still want to change the system time, however, you would need to send a command to the shell using shell_exec()
在shell中发送命令在Windows中,代码如下所示:
In Windows, the code would look like this:
shell_exec("date 09-09-99"); // Use "date mm-dd-yy" or "time hh:mm:ss", respectively
在UNIX中,根据日期
手册页,代码将如下所示:
In UNIX, according to the date
man page, the code would look like:
shell_exec("date 0909hhmm1999"); // It says "date MMDDhhmiYYYY". I'm not sure how to set seconds, although I assume "mi" = "minutes"
这篇关于更改今天的日期和时间在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!