首页 > 解决方案 > 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);

标签: phphtmlmysql

解决方案


你在 DateTime 类中有一个转换方法,所以你可以用这个替换 strtotime()

$timestamp1 = $qa_date->getTimestamp();
$timestamp2 = $sale_date->getTimestamp();

推荐阅读