A package to control the flow of time

Posted on

In this article we will learn about some of the frequently asked Php programming questions in technical like A package to control the flow of time. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error handling in PHP is simple. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in PHP. Below are some solution about A package to control the flow of time.

Imagine you’re building that your app can notify your user, but you don’t want to send more than one notification in a timeframe of five seconds. How are you going to test the time aspect? Do you have to create a test that takes five minutes?

Luckily the answer is “no”. If you’re using the popular Carbon library, you can set the value that the library considers “now”. You can do this using Carbon::setTestNow($now) where $now is a instance of CarbonCarbon.

In the test suites of my projects and package I often added code like this:

protected function setNow(string $time, $format = 'Y-m-d H:i:s')
    $now = Carbon::createFromFormat($format, $time);


protected function progressSeconds(int $seconds)
    $newNow = now()->addSeconds($seconds);


protected function progressMinutes(int $minutes)
    $newNow = now()->addMinutes($minutes);


protected function progressHours(int $hours)
    $newNow = now()->addHours($hours);


Because I was tired of coding this over and over again, I decided to a new small, handy package called spatie/test-time. Using the package, you can freeze the time with:

// time will not progress anymore


Alternatively, you can pass in a carbon instance that will be used as the current time.


There’s also no need to add separate functions like progressSeconds, progressMinutes and so on… You can progress the time with any of the carbon functions starting with add or sub.



// you can also chain calls

Here’s an example where I used it in a real world project:

Dogfooding myself with the newly released spatie/test-time package ⏳https://t.co/L0clvjrgRI#php #testing pic.twitter.com/xkDFaXpTsf

— Freek Van der Herten ? (@freekmurze
) June 12, 2019

Happy tinkering with the time!

Leave a Reply

Your email address will not be published.