php 在php中更改今天的日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3923848/
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
Change today's date and time in php
提问by vooD
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.
是否可以更改 php 的当前日期和时间(类似于 set_time(strtotime('1999-09-09')),所以当我调用 time() 时,它会返回 1999-09-09 的时间戳?我需要设置它全局适用于整个应用程序。
EDIT
编辑
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.
我已经有一个旧的应用程序,我需要设置当前日期。我不能接受任何自定义功能。想象一下,我应该检查所有代码并替换每次()调用。即使我会这样做,也无济于事,因为我也需要替换像 getdate() 这样的函数。
采纳答案by stevendesu
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:
这些time()
函数实际上向系统时钟询问时间,因此您必须更新服务器系统时钟的时间。需要牢记以下几点:
- This is done differently on a Unix server than a Windows server. If you want code compatible with both, you have to be careful
- Sometimes (especially in shared hosting situations) you aren't allowed to change the system time
- Changes to system time will remain after the script has executed unless you change it back later. Therefore you risk really screwing up the system clock.
- 这在 Unix 服务器上与在 Windows 服务器上不同。如果您希望代码与两者兼容,则必须小心
- 有时(尤其是在共享主机情况下)您不允许更改系统时间
- 脚本执行后,系统时间的更改将保留,除非您稍后将其更改回来。因此,您冒着真正搞砸系统时钟的风险。
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:
如果您想全局更改整个应用程序的时间,我建议使用静态变量$timeOffset
并创建您自己的time()
函数,如下所示:
function myTime(){
global $timeOffset;
return time()+$timeOffset;
}
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 发送命令 shell_exec()
In Windows, the code would look like this:
在 Windows 中,代码如下所示:
shell_exec("date 09-09-99"); // Use "date mm-dd-yy" or "time hh:mm:ss", respectively
In UNIX, according to the date
man page, the code would look like:
在 UNIX 中,根据date
手册页,代码如下所示:
shell_exec("date 0909hhmm1999"); // It says "date MMDDhhmiYYYY". I'm not sure how to set seconds, although I assume "mi" = "minutes"
回答by zmf
You could also check the Timecop PHP module, worked perfectly in my case.
您还可以检查Timecop PHP 模块,在我的情况下效果很好。
After installing it, you can travel back & forth in time with just one line of code.
安装后,您只需一行代码即可在时间来回穿梭。
回答by The Pellmeister
PHP doesn't have a date and time on its own. time() returns the current timestamp of the server's time. Depending on your operating system, it may well be that you (i.e. the user that runs your script's process) are actually not allowed to change the server date and time.
PHP 本身没有日期和时间。time() 返回服务器时间的当前时间戳。根据您的操作系统,很可能您(即运行脚本进程的用户)实际上不允许更改服务器日期和时间。
Which asks a different question: Why do you want to do this?
这提出了一个不同的问题:你为什么要这样做?
Better would be to implement your own time() function:
更好的是实现你自己的 time() 函数:
class myTime
{
private static $offset = 0;
public static function getTime()
{
return time() + self::$offset;
}
public static function setTime($time)
{
self::$offset = $time - time();
}
}
And then, everywhere where you use time()
, use myTime::getTime();
. To set the time, use myTime::setTime($time)
where $time
is the timestamp you want to use.
然后,在您使用 的任何地方time()
,使用myTime::getTime();
. 要设置时间,使用myTime::setTime($time)
其中$time
的时间戳要使用。
回答by Spudley
No it isn't. PHP gets its time from the system time. The only way you can change what PHP sees as the time is to change the system time.
不,不是。PHP 从系统时间获取时间。您可以更改 PHP 看到的时间的唯一方法是更改系统时间。
It maybe feasible for you to do that using exec()
or something similar, but probably not advisable.
这可能是可行的你,使用做exec()
或类似的东西,但可能不可取。
A better solution may be to create your own time function, which you can specify, using an offset.
更好的解决方案可能是创建您自己的时间函数,您可以使用偏移量指定它。
eg:
例如:
function mytime() {
return time()+set_mytime();
}
function set_mytime($new_time=null) {
static $offset;
if(!$new_time) {return $offset;}
//$new_time should be a unix timestamp as generated by time(), mktime() or strtotime(), etc
$real_time=time();
$offset=$real_time-$new_time;
}
//usage:
set_mytime(strtotime("1999-09-09");
$faketime=mytime();
.... do something that takes a bit of time ....
$faketime2=mytime();
That's just something I've thrown together quickly for you. I haven't tested it, so there may be (probably are) bugs, but I hope it gives you enough to get a solution.
这只是我为你迅速拼凑起来的东西。我还没有测试过它,所以可能有(可能是)错误,但我希望它能给你足够的解决方案。
回答by kenorb
You may also try to use global $_SERVER['REQUEST_TIME']
variable (available since PHP 5.1.0) and replace it on run-time.
您也可以尝试使用全局$_SERVER['REQUEST_TIME']
变量(自 PHP 5.1.0 起可用)并在运行时替换它。