php - strtotime() 期望参数 1 是字符串,时间戳对象
问题描述
我目前正在尝试比较两个日期以确定差异。数据是从mysql服务器中提取出来的。但是,我不断收到此错误:
strtotime() 期望参数 1 是字符串,给定对象
我尝试过一些人们以前厌倦的步骤,但它不起作用。
$qa_date= new DateTime ($row["fa_timestamp"]);
$sale_date=new DateTime ($row["time_enter"]);
$timestamp1 = strtotime($qa_date);
$timestamp2 = strtotime($sale_date);
$weekend = array(0, 6);
if(in_array(date("w", $timestamp1), $weekend) || in_array(date("w", $timestamp2), $weekend))
{
return 0;
}
$diff = $timestamp2 - $timestamp1;
$one_day = 60 * 60 * 24; //number of seconds in the day
if($diff < $one_day)
{
return floor($diff / 3600);
}
$days_between = floor($diff / $one_day);
$remove_days = 0;
for($i = 1; $i <= $days_between; $i++)
{
$next_day = $timestamp1 + ($i * $one_day);
if(in_array(date("w", $next_day), $weekend))
{
$remove_days++;
}
}
$diff2= floor(($diff - ($remove_days * $one_day)) / 3600);
解决方案
你在 DateTime 类中有一个转换方法,所以你可以用这个替换 strtotime()
$timestamp1 = $qa_date->getTimestamp();
$timestamp2 = $sale_date->getTimestamp();
推荐阅读
- dns - dig新旧版本edns参数的区别
- r - 如何删除在两列中具有相同值的行以及与先前观察重复的行,而不管顺序如何
- lua - 如何在排行榜中添加标题?ROBLOX LUA
- typescript - 如何在打字稿中使用代理?
- kubernetes - 如何识别 GKE 抢占节点事件
- c++ - OpenGL 和 GLM 矩阵无法正确缩放,总是按比例缩小
- php - 从 PHP 中的函数返回值时无法获得结果
- python - 在 django 设置中设置一个变量,条件是 dockerized
- c# - 选择另一个项目时,先前选择的项目的 UWP NavigationView 前景色不会更新
- unit-testing - 在 Flutter 中使用 StreamSubscription 测试 BLoC