Fork of original package maintained by Wider Plan.
A PHP extension providing "time travel" and "time freezing" capabilities, inspired by ruby timecop gem.
git clone https://github.com/kiddivouchers/php-timecop.git
cd php-timecop
phpize
./configure
make
make install
After install, add these lines to your php.ini
.
extension=timecop.so
- OS: Linux, macOS
- PHP: 5.6.x - 8.3.x
- SAPI: Apache, CLI
- Other SAPIs are not tested, but there is no SAPI-dependent code.
- non-ZTS(recommended), ZTS
- Freeze time to a specific point.
- Travel back to a specific point in time, but allow time to continue moving forward from there.
- Scale time by a given scaling factor that will cause time to move at an accelerated pace.
- Override the following PHP stock functions and methods, which supports freezing or traveling time.
time()
mktime()
gmmktime()
date()
gmdate()
idate()
getdate()
localtime()
strtotime()
strftime()
gmstrftime()
microtime()
gettimeofday()
unixtojd()
DateTime::_construct()
DateTime::createFromFormat()
(PHP >= 5.3.0)DateTimeImmutable::_construct()
(PHP >= 5.5.0)DateTimeImmutable::createFromFormat()
(PHP >= 5.5.0)date_create()
date_create_from_format()
(PHP >= 5.3.0)date_create_immutable()
(PHP >= 5.5.0)date_create_immutable_from_format()
(PHP >= 5.5.0)
- Rewrite value of the following global variables when the time has been moved.
$_SERVER['REQUEST_TIME']
<?php
var_dump(date("Y-m-d")); // todays date
timecop_freeze(0);
var_dump(gmdate("Y-m-d H:i:s")); // string(19) "1970-01-01 00:00:00"
var_dump(strtotime("+100000 sec")); // int(100000)
timecop_freeze()
is used to statically mock the concept of now. As your program executes, time()
will not change unless you make subsequent calls to timecop_freeze/travel/scale/return
. timecop_travel()
, on the other hand, computes an offset between what we currently think time()
is and the time passed in. It uses this offset to simulate the passage of time. To demonstrate, consider the following code snippets:
<?php
$new_time = mktime(12, 0, 0, 9, 1, 2008);
timecop_freeze($new_time);
sleep(10);
var_dump($new_time == time()); // bool(true)
timecop_return(); // "turn off" php-timecop
timecop_travel($new_time);
sleep(10);
var_dump($new_time == time()); // bool(false)
timecop_scale($scaling_factor)
make time move at an accelerated pace. With this function, you can emulate long-span integration test and reduce execution time of time-dependent unit test.
<?php
Timecop::freeze(new DateTime("2017-01-01 00:00:00"));
Timecop::scale(50); // time goes x50 faster
usleep(100000); // 100ms
var_dump((new DateTime())->format("c")); // string(25) "2017-01-01T00:00:05+00:00"
- Support PHP 8.3
- Support PHP 8.2
- Support PHP 8.1
- Fix reflection mismatches between native PHP and timecop overrides
- Fixes problems when non standard timezone identifiers are used. (#52)
- Support PHP 8.0
- Fixed memory leak
- Code cleanup
- Support PHP 7.4.
- Fix incorrect class returned with DateTimeImmutable::createFromFormat() and pipe character.
- Fix test failure when using PHP 7.3 (#42)
- Fix some ZTS issues (PR #45)
- Ensure strict types are respected (#43)
- Fix "double free" bug on PHP 7.2.0 (#32)
- Publish on PECL
- Support Windows (experimental)
- Bug fixed: Calling timecop_freeze() on a fast machine sometimes fails to stop the microsecond part of current time.
- Support PHP 7.2.0+
- Fix #18 (Fix date_create_from_format when using
|
char) - Fix
timecop_date_create()
: The previous version oftimecop_date_create("@[unix timestamp]")
returns +1 hour time on PHP 5.3.9 - 5.4.7 only during the DST.
- Fix
timecop_date_create_from_format()
: support time travelling- Now portions of the generated time not provided in
format
will be set to the travelled time - The previous version is completely same behavior as
date_create_from_format()
.
- Now portions of the generated time not provided in
- Remove
TimecopDateTime::getTimestamp()
,TimecopDateTime::setTimestamp()
on PHP 5.2.x
- Implement
TimecopDateTimeImmutable
class andtimecop_date_create_immutable()
,timecop_date_create_immutable_from_format()
functions. - Now
timecop_date_create_from_format()
returnsDateTime
instance
- Big internal change (without BC break): handle microseconds accurately in time traveling.
- Now
timecop_freeze()
andtimecop_travel()
accepts eitherDateTimeInterface
orint
.- With
DateTimeInterface
argument, freezed/traveled time would have fraction of second.
- With
- Implement
timecop_scale()
: Make time go faster. - Implement
Timecop::***()
as alias oftimecop_***()
. (Forfreeze
,travel
,return
,scale
)
- Fix crash when non-object passed as 2nd argument of TimecopDateTime::__construct() (Fix #9)'
- Add CI environment (CentOS, Ubuntu 32-bit, PHP 7.1)
- Fix for stock PHP on Ubuntu
- Support PHP 7.0.x
- Now
new DateTime()
always returnsDateTime
instance- The previous version returns
TimecopDateTime
instance whentimecop.func_override=1
.
- The previous version returns
- Implement
timecop_gettimeofday()
andtimecop_microtime()
- Fix #10 (Timecop segfaults when set_error_handler throws an exception)
- Fix
TimecopDateTime::createFromFormat()
to reutrnTimecopDateTime
instance on PHP >= 5.3.4- The previous version returns
DateTime
instance - Implement identical function
timecop_date_create_from_format()
- BUG: not supporting "relative formats" for this method currently.
- The previous version returns
- Fix behavior of
TimecopDateTime::_construct()
when its 2nd argument is specified.
- Fix SIGSEGV in
TimecopDateTime::__construct()
called with NULL as 1st argument
- Fix the time traveling implementation for
TimecopDateTime::__construct()
- Fix
timecop_date_create()
to returnTimecopDateTime
instance- The previous version returns
DateTime
instance
- The previous version returns
- Add
TimecopDateTime::getTimestamp()
,TimecopDateTime::setTimestamp()
only for PHP 5.2.x
- Implement
timecop_date_create()
- Implement time traveling feature for
TimecopDateTime::__construct()
with 1 or 2 arguments- The previous version works correctly only for no arguments calling: "new TimecopDateTime()"
- Fix memory leak
- Initial Release
The MIT License
Copyright (c) 2012-2017 Yoshio HANAWA Copyright (c) 2019-2024 Wider Plan Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.