Created: 2011-07-14 07:11
Updated: 2016-05-27 06:00
License: mit



Time testing php library inspired by ruby timecop gem.

Timecop-PHP provides wrappers around date/time functions:

  • time (Timecop::time())
  • date (Timecop::date())
  • getdate (Timecop::getdate())
  • gmdate (Timecop::gmdate())
  • gmmktime (Timecop::gmmktime())
  • gmstrftime (Timecop::gmstrftime())
  • idate (Timecop::idate())
  • localtime (Timecop::localtime())
  • mktime (Timecop::mktime())
  • strftime (Timecop::strftime())
  • strptime (Timecop::strptime())
  • strtotime (Timecop::strtotime())

You can override built-in functions in cases where it's not possible to replace parts of code using PHP's built-in functions like time() or date() with Timecop wrappers. To do so runkit pecl extension must be installed and working. See installation section for more details.


// override internal PHP functions

// time travel
$presentTime = time();
Timecop::travel(time() - 3600); // one hour back

$presentTime > time(); // TRUE

Timecop::travel(time() + 7200); // one hour forward
$presentTime < time(); // TRUE

// freeze time
$frozenTime = time();

$frozenTime == time(); // TRUE

// restore time - unfreezes time and returns to present

$frozenTime == time(); // FALSE
$frozenTime > time(); // TRUE

// restore original PHP functionality


Requires runkit PECL module to override PHP internal functions. Current pecl version does not work with PHP 5.2+ therefore I recommend you to compile your own from sources.

Sources in PHP svn haven't been worked on for a while now so grab ones on the github instead:

$ git clone https://github.com/zenovich/runkit
$ cd ./runkit
$ phpize
$ ./configure && make
$ make test
$ make install

Note: make test failed and/or skipped every test on CentOS 5 with 64bit PHP 5.2.10 but Timecop still worked. YMMV.

You need to enable runkit extension and override flag in php.ini:

runkit.internal_override = 1
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more