PHP - 设置/模拟假日期 [英] PHP - Setting up/emulating fake date

查看:127
本文介绍了PHP - 设置/模拟假日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。在PHP脚本工作的时候可以设置假日期吗?我正在使用使用时间段的API,例如当您发送请求获取一些数据时,您可以指定时间段(例如一个月,但这些时间段是固定的),并将其结果从request_time-1month返回给request_time。显然,请求数据是现在的时间,所以我想效仿现在的时间(例如设定日期从5月10日到4月15日)。可能吗?我在PHP文档中看不到任何有用的东西,我甚至不知道它是否可以工作,因为我不知道这个API是否使用来自服务器的时间,或存储上述数据的独立服务器。但是也许有人有任何想法。



从提前感谢

解决方案

如果您正在连接的API正在使用request_time - 1个月,那么您的脚本认为时间是不相关的,只是API后面的脚本认为是。 (作为一个愚蠢的比喻:如果一个商店在下午5点关闭,早点设置你的手表不会让你在5点30分;你需要潜入和更换墙上的时钟,以免工作人员锁定)



所以,除非你可以找到一种方法来愚弄远程API,认为你在不同的时间发送了请求,没有什么可以做的。 p>

如果您控制API,那么只需编辑API即可使用超过now定义的参数,也许只能在特殊的调试模式下使用。 p>

I have a bit weird question. Is it possible to set up fake date in PHP for the time in which script is working? I am using API which uses time periods, for example when you send request to get some data, you can specify time period (for example one month, but these periods are fixed) and it will return results from request_time-1month to request_time. Obviously, request data is present time, so I want to emulate this present time (for example set date from 10 May to 15 April). Is it possible? I can't see anything helpful in PHP documention and I don't even know if it would work, because I'm not sure if this API uses time from server in which it works or independent server which stores aforementioned data. But maybe someone has any idea.

Thanks from advance.

解决方案

If the API you are connecting to is using "request_time - 1 month", then what your script thinks the time is isn't relevant, only what time the script behind the API thinks it is. (As a silly analogy: if a shop closes at 5PM, setting your watch early won't let you in at 5:30; you'd need to sneak in and change the clock on the wall so that the staff don't lock up.)

So unless you can find a way to fool the remote API into thinking you sent the request at a different time, there is nothing you can do.

If you control the API, then just edit the API to have a parameter which over-rides the definition of "now", perhaps only allowed in a special debug mode.

这篇关于PHP - 设置/模拟假日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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