LoginSignup
7
8

More than 5 years have passed since last update.

php-timecopで時間を仮想的に変化させる

Posted at

環境

  • Docker
  • Amazon Linux
  • PHP 5.6.x

インストール

$ git clone https://github.com/hnw/php-timecop.git
$ cd php-timecop
$ phpize
$ ./configure
$ make
$ make install

gccとかphp56-develが入ってなかったので、phpizeでつまづきました。

php.iniに追記

php -i | grep php.iniでphp.iniの場所を調べて下記を追記。

extension=timecop.so

試してみる

<?php
date_default_timezone_set("Asia/Tokyo");
echo date("Y-m-d H:i:s")."\n";

echo "\ntimecop_freeze\n";
timecop_freeze(new DateTime("2017-03-31 10:00:00"));
echo date('Y-m-d H:i:s'). "\n";

実行結果

2017-04-18 17:31:55

timecop_freeze
2017-03-31 10:00:00

使えそうな関数

関数名 機能
timecop_freeze PHPの現在時刻を変更する(時刻不変)
timecop_travel PHPの現在時刻を変更する(時間は変わっていく)
timecop_return PHPの現在時刻を元に戻す

参考

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8