Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对非对象上的成员函数setTime()的php日期调用_Php - Fatal编程技术网

对非对象上的成员函数setTime()的php日期调用

对非对象上的成员函数setTime()的php日期调用,php,Php,我正试图改变我约会时间的时间,而且我正在做正确的事情。但是,一定有什么我不理解的,因为调用该函数时,我在非对象上调用成员函数setTime()时遇到了错误 这是我的代码: $date = date("Y-m-d H:i:s"); $date_in_secs = strtotime($date); $new_date = $date_in_secs + $delivery_duration + $distribution_time; $date = date('l d F H:i Y', $ne

我正试图改变我约会时间的时间,而且我正在做正确的事情。但是,一定有什么我不理解的,因为调用该函数时,我在非对象上调用成员函数setTime()时遇到了错误

这是我的代码:

$date = date("Y-m-d H:i:s");
$date_in_secs = strtotime($date);
$new_date = $date_in_secs + $delivery_duration + $distribution_time;

$date = date('l d F H:i Y', $new_date);

if (intval(date('H', $new_date)) > 21 || intval(date('H', $new_date)) < 8) {
    $date->setTime(8, 0, 0);
    $date->add(new DateInterval('P1D'));
} 
$date=日期(“Y-m-d H:i:s”);
$date_in_secs=标准时间($date);
$new_date=$date_in_secs+$delivery_duration+$distribution_time;
$date=日期('l d F H:i Y',$new_date);
如果(intval(日期('H',$new_日期))>21(日期('H',$new_日期))<8){
$date->setTime(8,0,0);
$date->add(新的日期间隔('P1D'));
} 
关于如何更改日期小时并添加一天的任何线索?

来自文档:

公共日期时间日期时间::设置时间(int$hour,int$minute[,int $second=0])

在等待用逗号分隔的int时,您正在放置字符串。请尝试以下操作:


$date->setTime(8,0,0')

什么是
$delivery\u duration
$distribution\u time
的内容/类型

你为什么要这么做

$date = date("Y-m-d H:i:s"); // Convert DateTime to String
$date_in_secs = strtotime($date); // Convert String to Timestamp
为什么不只用DateTime?更干净

$date = new DateTime;
$date->modify('+200 seconds'); // Increase with 200 seconds from $delivery_duration for example

if (intval($date->format('H')) > 21 || intval($date->format('H')) < 8) {
    $date->setTime(8); // Set Hour to 8
    $date->modify('+1 day'); // Increase day by 1
}

$dateString = $date->format('Y-m-d H:i:s');
$date=新的日期时间;
$date->modify(“+200秒”);//例如,从$delivery\u duration开始增加200秒
如果(intval($date->format('H'))>21(intval($date->format('H'))<8){
$date->setTime(8);//将小时设置为8
$date->modify(“+1天”);//增加1天
}
$dateString=$date->format('Y-m-dh:i:s');

你把字符串(
date()
return)和对象(
newdatetime()
混在一起了)。你是什么意思?我应该更改什么?$date->setTime(8,0,0)<代码>$date\u in_secs=strotime
毫无意义
$date\u in_secs=time()
,而不会迫使PHP浪费大量cpu周期获取unix时间戳,将其转换为字符串,然后将该字符串转换回与它开始时完全相同的时间戳,因为当我在服务器中复制代码时,我没有任何错误,$date=星期四2011年11月27日19:10,我得到了对成员函数setTime()的
调用在非对象上
很抱歉,我认为我无法帮助解决此问题,因为这段代码对我有效,可能问题不在这一行中。是的,它正好在
setTime
调用中,因为该块被包装在try-catch块中,并且我得到了对成员函数setTime()的错误
调用在非对象上
会出现错误,因为您正在对
字符串
调用
日期时间
方法。。。在if语句之前,您正在设置
$date=date('ldfh:iy',$new\u date)
输出一个
字符串
。我需要将日期转换为一个时间戳以相加秒数,注意行
$new\u date=$date\u in_secs+$delivery\u duration+$distribution\u time只需使用或将其添加到
日期时间
。将
$delivery\u duration
$distribution\u time
更改为仅秒。然后我被迫再次使用
strrotime
来获取小时数,我不明白为什么。我刚刚更新了答案,几乎与你的答案一致。现在,您只需找到一种方法,以秒为单位获取
$delivery\u duration
$distribution\u time
,即可完成。