PHP - setting / emulating fake date

I have a slightly strange question. Is it possible to set a fake date in PHP while the script is running? I use an API that uses time periods, for example, when you send a request for some data, you can specify a time period (for example, one month, but these periods are fixed), and it will return results from request_time-1month to request_time. Obviously, the request data is present, so I want to imitate this time (for example, set the date from May 10 to April 15). Is it possible? I don’t see anything useful in PHP docs, and I don’t even know if this will work, because I’m not sure whether this API uses time from the server it is running on, or on an independent server that stores the above data. But maybe someone has an idea.

Thanks in advance.

+4
source share
4 answers

If the API you are connecting to uses "request_time - 1 month", then your script considers that time does not matter, only at that moment when the script behind the API looks like this. (How stupid analogy: if the store closes at 5 pm, setting the early hours will not allow you at 5:30, you need to sneak and change the clock on the wall so that the staff do not block until.)

Therefore, if you cannot find a way to trick the remote API into thinking that you sent the request at another time, you cannot do anything.

If you control the API, simply edit the API to have a parameter that has moved the definition of "now", perhaps only allowed in special debug mode.

+1
source

Use libfaketime ( https://github.com/wolfcw/libfaketime ). On ubuntu - just set faketime using apt-get or aptitude.

  • If you want to fake time for a console application:

    LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1 FAKETIME="2008-01-01 00:00:00" php yourscript.php 
  • If you want to fake Apache time, you will need to start apache with libfaketime preloaded. Just put

     export FAKETIME="2008-01-01 00:00:00" export LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1 

in / etc / apache2 / envvars and restart apache. Now your server response will always look like

 $ curl -D - http://localhost HTTP/1.1 200 OK Date: Tue, 01 Jan 2008 00:00:00 GMT Server: Apache/2.2.22 (Ubuntu) Last-Modified: Tue, 01 Jan 2008 00:00:00 GMT ETag: W/"420419-b1-4429c2a6653ac" Accept-Ranges: bytes Content-Length: 177 Vary: Accept-Encoding Content-Type: text/html 
+9
source

Effectively, the date and time through which you access through PHP is the server’s system time. Therefore, if you are able to change this, you can achieve what you need. Have a look at this answer: fooobar.com/questions/340886 / ...

Although personally, I would recommend using a function that returns -30 days (for example) from the current date. If you want to change the date back, replace the function with real getdate() .

0
source

What exactly are you trying to do? If you are experiencing something, you should not rely on the system configuration at all - it is better to do something like:

 my_function(\DateTime $now = null) { if($now === null) { return new \DateTime(); } else { return $now; } } 

In your regular code, you just call my_function() , but in your test, you call

 $now = \DateTime::createFromFormat(...); my_function($now); 

A similar idea if this is for production code. Pass the date as a parameter, instead of leaving the function to receive it.

 my_function(\DateTime $datetime) { // does stuff } call_my_function() { my_function(new \DateTime()); } other_way_to_call_my_function() { $datetime = \DateTime::createFromFormat(...); my_function($datetime); } 
0
source

Source: https://habr.com/ru/post/1480097/


All Articles