How to test with specific time in Laravel

Categorized as Laravel Tagged

Hello friends. Sometimes you need to run tests that rely on specific time in the past or in the future. There is a useful trick to fake time for that purpose. Just set fake timestamps using now() helper or Carbon class. Here is the example:

// Fake current time
Carbon::setTestNow($timestamp);
now()->setTestNow($timestamp);

// Reset faked time
Carbon::setTestNow();
now()->setTestNow();

Leave a reply

Your email address will not be published.