laravel 接收 strtotime() 期望参数 1 为字符串,对象在 php 中给出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28008088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:43:36  来源:igfitidea点击:

Receiving strtotime() expects parameter 1 to be string, object given error in php

phpdatelaravel

提问by user3127109

The program works fine when I pass the array like following:

当我像下面这样传递数组时,程序运行良好:

$holidays=array("2015-01-01","2015-01-05","2015-01-10");

echo getWorkingDays("2015-01-02","2015-01-09",$holidays);

But when I extract the data from database like this:

但是当我像这样从数据库中提取数据时:

$holidays = DB::table('tbl_holiday')->select('holiday_date')->whereBetween('holiday_date', array('2015-01-01', '2015-01-20'))->get();

I am getting this error: strtotime() expects parameter 1 to be string, object given

我收到此错误:strtotime() 期望参数 1 为字符串,给定的对象

In my helpers.php, I have this code

在我的 helpers.php 中,我有这个代码

 foreach($holidays as $holiday){

       $time_stamp=strtotime($holiday);

      //If the holiday doesn't fall in weekend
      if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 )
      $workingDays--;
}

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by lukasgeiter

Just get the actual datestring from $holiday

只需从中获取实际的日期字符串 $holiday

$timestamp = strtotime($holiday->holiday_date);

回答by patricus

Using the database query, your $holidaysvariable is an array of stdClass objects. If you want an array that contains just the holiday_datevalues, you can use the lists()method, instead of get():

使用数据库查询,您的$holidays变量是一个 stdClass 对象数组。如果你想要一个只包含holiday_date值的数组,你可以使用该lists()方法,而不是get()

$holidays = DB::table('tbl_holiday')
    ->whereBetween('holiday_date', array('2015-01-01', '2015-01-20'))
    ->lists('holiday_date');