Invoice :: setDueDate()必须使用calcinai实现接口DateTimeInterface,Xero API

hun*_*ala 3 php wrapper xero-api

我正在关注这个包装器

我有这样的错误:开捕致命错误:传递给XeroPHP \型号\会计\发票:: setDueDate(参数1)必须实现接口DateTimeInterface,字符串中给定

这是我的代码:

try{
            $lineitem = new LineItem($this->_xi);
            $lineitem->setAccountCode('200')
            ->setQuantity('5.400')
            ->setDescription('this is awesome test')
            ->setUnitAmount('9900.00');

            $contact = new Contact($this->_xi);
            $contact->setName("John Doe")
                ->setFirstName("John")
                ->setLastName("Doe")
                ->setEmailAddress("johngwapo@hot.com")
                ->setContactStatus(Contact::CONTACT_STATUS_ACTIVE);



            $invoice = new Invoice($this->_xi);
            $invoice->setType(Invoice::INVOICE_TYPE_ACCREC)
                ->setStatus(Invoice::INVOICE_STATUS_AUTHORISED)
                ->setContact($contact)
                //->setDate(\DateTimeInterface::format("Y-m-d"))
                ->setDueDate("2018-09-09")
                ->setLineAmountType(Invoice::LINEAMOUNT_TYPE_EXCLUSIVE)
                ->addLineItem($lineitem)
                ->setInvoiceNumber('10')
                ->save();



        }catch ( Exception $e ){
            $GLOBALS['log']->fatal('[Xero-createContact]-' . $e->getMessage());
            echo $e->getMessage();

        }
Run Code Online (Sandbox Code Playgroud)

当我尝试这样做时:

->setDueDate(\DateTimeInterface::format("Y-m-d"))
Run Code Online (Sandbox Code Playgroud)

我得到了这个错误:致命错误:非静态方法DateTimeInterface :: format()无法静态调用,假设$ this来自不兼容的上下文

这是我调用的setDueDate的功能:

 /**
     * @param \DateTimeInterface $value
     * @return Invoice
     */
public function setDueDate(\DateTimeInterface $value)
    {
        $this->propertyUpdated('DueDate', $value);
        $this->_data['DueDate'] = $value;
        return $this;
    }
Run Code Online (Sandbox Code Playgroud)

我真的迷失了如何使用这个DateTimeInterface以及如何使用它来设置未来的日期,以及如何解决所有这些错误.

Ako*_*s K 9

第一个错误说,该->setDueDate($date)方法需要一个实现DateTimeInterface的对象,但您只提供了一个字符串->setDueDate("2018-09-09")

第二个错误说,该format($format)方法不能静态调用.它需要一种格式模式,并根据提供的模式将现有对象格式化为字符串.但是你试图静态地调用它,提供日期字符串而不是格式模式 - 难怪它失败了.您需要使用该createFromFormat($format, $date_string)方法,该方法从字符串创建DateTime对象,而不是相反.

解决方案是创建一个实现DateTimeInterface的对象.例如DateTimeDateTimeImmutable (它是相同的,但永远不会被修改).如果您稍后可以修改此值,我建议使用DateTime.

所以改变这一行:
->setDueDate("2018-09-09")
对此:
->setDueDate(\DateTime::createFromFormat('Y-m-d', "2018-09-09"))
它应该像魅力一样工作.