avadev / AvaTax-Calc-REST-PHP

PHP sample for the AvaTax Calc REST API
Apache License 2.0
9 stars 28 forks source link

ErrorException: Call to a member function format() on boolean /AvaTax/GetTaxRequest.php [ 44 ] #30

Open shiloanton opened 8 years ago

shiloanton commented 8 years ago

DateTime returns boolean "$date = \DateTime::createFromFormat('U.u', microtime(true));" , no object, since can not convert the input parameters to DateTime object.

ErrorException [ 1 ]: Call to a member function format() on boolean /AvaTax/GetTaxRequest.php [ 44 ]

skylord123 commented 8 years ago

I cannot reproduce this issue. What PHP version are you running? Need more information to test.

skylord123 commented 8 years ago

Does changing

$date = \DateTime::createFromFormat('U.u', microtime(true));

to

$date = new \DateTime();

Fix the issue?

I don't know why the code is using microtime. The DocDate is a date not date and time. Creating a new \DateTime object without a parameter will create it off the current time anyways. Even if we were using the time of day as well we wouldn't need to use microtime.. No point in that much accuracy.

estevejm commented 8 years ago

We're having the same issue with PHP 5.6.5, we were not able to reproduce it so far.

About using that much accuracy, the microtime information is not used in DocDate but it's being used in DocCode.

public function __construct()
{
    $date = \DateTime::createFromFormat('U.u', microtime(true));
    $this->DocDate = $date->format('Y-m-d');
    ...
    $this->DocCode = $date->format('Y-m-d-H-i-s.u');                        
}

I don't know why a DateTime is being used to identify the DocCode by default, maybe generating a unique id would be enough and would allow us to generate the DateTime without the microtime as @skylord123 suggested.

estevejm commented 8 years ago

Finally we were able to reproduce it. Extracted from http://php.net/manual/en/datetime.createfromformat.php

Reportedly, microtime() may return a timestamp number without a fractional part if the microseconds are exactly zero.  I.e., "1463772747" instead of the expected "1463772747.000000".  number_format() can create a correct string representation of the microsecond timestamp every time, which can be useful for creating DateTime objects when used with DateTime::createFromFormat():

<?php
$now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''));
var_dump($now->format('Y-m-d H:i:s.u')); // E.g., string(26) "2016-05-20 19:36:26.900794"

We will create a PR just now

TkachukRuslan commented 8 years ago

Hi Could you please update the status of this issue Waiting your response.

estevejm commented 8 years ago

@TkachukRuslan the issue was reproduced and a PR that fixes it was opened (#35). We're now waiting it to be merged.