Local Community

How to change current date for PHP tests of date() function

For testing the script, what is the best way to temporarily change the date when running most new local ?

so that php date() would return certain date. Is there another way than change my mac’s date ?

I don’t think there’s a good way of doing what you are trying to do.

Instead of changing the system date to account for unit tests, you’ll likely want to re-write the functions so that you can pass in the actual time.

This isn’t something related to Local, but I think the solution you’re wanting would be similar to what’s outlined in this thread on StackOverflow, where you are passing in the “date” to the function, and that function contains logic to either use that date or create a new, current date object:

    function changeStartEndDate($now) {
        if (date('j', strtotime("now", $now), $now) === '1') {
            // ...
            $this->startDate = date("Y-n-j", strtotime(date("Y-m-01", $now), $now));
            $this->endDate = date("Y-n-j", strtotime("yesterday", $now), $now);
        }
    }

Thanks!

Just found out that changing the system time does not work because of ssl certificates. And whole wp-site is set to use ssl…too complicated

when using virtual machines before and no ssl, it was easy just to change time and done.

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.