Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 339

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 392

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 394
php调整时间为某个指定时间含数据库时间以生成测试数据 - Joyber 的博客

PHP方案:安装PHP扩展timecop

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

#PHP配置中增加
extension=timecop.so

验证

<?php
var_dump(extension_loaded('timecop')); // bool(true)
timecop_travel('2025-01-01');
echo date('Y-m-d'); // 2025-01-01

数据库时间:设置会话时间戳

$pdo = new PDO("mysql:host=127.0.0.1;dbname=test;charset=utf8mb4", "root", "");

// 让当前会话时间固定为 2025-01-01 00:00:00
$fakeTime = strtotime("2025-01-01 00:00:00");
$pdo->exec("SET @@session.timestamp = $fakeTime");

// 测试
$stmt = $pdo->query("SELECT NOW() AS now, UNIX_TIMESTAMP() AS ts");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row); // 输出你设置的假时间

标签: none

添加新评论