https://github.com/potherca-blog/php-timecop-example
http://blog.pother.ca/php-timecop-example/ Examples demonstrating how PHP TimeCop works
https://github.com/potherca-blog/php-timecop-example
blog-article example example-app example-code example-codes example-project example-projects example-repo examples examples-php php php-example php-examples php-extensions php-module php-modules php-timecop potherca potherca-blog timecop
Last synced: 10 days ago
JSON representation
http://blog.pother.ca/php-timecop-example/ Examples demonstrating how PHP TimeCop works
- Host: GitHub
- URL: https://github.com/potherca-blog/php-timecop-example
- Owner: potherca-blog
- Created: 2020-04-20T08:57:33.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-06-01T23:41:28.000Z (about 2 years ago)
- Last Synced: 2025-11-29T23:09:55.097Z (7 months ago)
- Topics: blog-article, example, example-app, example-code, example-codes, example-project, example-projects, example-repo, examples, examples-php, php, php-example, php-examples, php-extensions, php-module, php-modules, php-timecop, potherca, potherca-blog, timecop
- Language: PHP
- Homepage: https://php-timecop.glitch.me/
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

> _"There is never enough time."_
> ~ Max Walker
[](https://glitch.com/edit/#!/remix/php-timecop)
## Introduction
The [php-timecop](https://github.com/hnw/php-timecop) PHP extension
makes it possible to manipulate time as experienced by the PHP runtime.
It provides "time travel" and "time freezing" capabilities.
This can be very useful when testing code that has time-sensitive functionality.
This project offers examples demonstrating how PHP TimeCop works.
It can be seen in action at https://php-timecop.glitch.me/
To play around with it, [remix this project on Glitch](https://glitch.com/edit/#!/remix/php-timecop).
## Usage
1. [Install php-timecop](https://github.com/hnw/php-timecop#install-with-package-manager) on a PHP server of your choice.
2. Clone this repository to the same server.
3. Visit the `index.php` in a web browser.
4. Study the provided examples and their output.
5. ...
6. [Profit!](http://knowyourmeme.com/memes/profit)
## Features
PHP TimeCop offers four functions to manipulate time.
For the static class method a regular function is also available (execept for `return` as that is a reserved word in PHP):
- `TimeCop::freeze($new_time)` / `timecop_freeze($new_time)`
- `timecop_return()`
- `TimeCop::scale($scaling_factor)` / `timecop_scale($scaling_factor)`
- `TimeCop::travel($new_time)` / `timecop_travel($new_time)`
### Freeze
Used to statically mock the concept of "now".
As the PHP runtime executes, `time()` will not change unless subsequent calls to freeze/return/scale/travel are made.
### Return
Return the system to a normal state. Effectively "turn off" php-timecop
### Scale
Make time move at an accelerated pace. With this function, long time spans can be emulated in a shorter time.
### Travel
Computes an offset between the currently think `time()` and the time passed in. It uses this offset to simulate the passage of time.
## Examples
The result of these examples can be seen below.
### Freeze
```php
format('Y-m-d H:i:s'));
Timecop::scale(86400); // time passes at one day per second
sleep(1);
var_dump((new DateTime())->format('Y-m-d H:i:s'));
sleep(1);
var_dump((new DateTime())->format('Y-m-d H:i:s'));
```
### Travel
```php