Placing a variable into $created_at = new \Square\Models\TimeRange();

$date=date(‘Y-m-d’);
$today_startdatetime = $date.‘T00:00:00+8:00’;
$today_enddatetime = $date.“T23:59:59+8:00”;

$created_at = new \Square\Models\TimeRange();
$created_at->setStartAt(’’$today_startdatetime’’); //not working
$created_at->setEndAt(‘2021-04-16T23:59:59+08:00’); //this works

$date_time_filter = new \Square\Models\SearchOrdersDateTimeFilter();
$date_time_filter->setCreatedAt($created_at);

So the above is my code where I replaced XXX inside the setStartAt(XXX) from String to a variable and it would not work. Is there any solution I could be able to place a variable inside the setStartAt()?

Error received: Uncaught InvalidArgumentException: JsonMapper::mapClass() requires second argument to be a class name, \Square\Models\Error given

Haven’t tested this myself, but you don’t need to put quotes around a variable (else it’ll be parsed as a string). So you should do something like:

$created_at->setStartAt($today_startdatetime);

if it’s a valid datetime.

Hi have tried your solution, but this is the error I have received:

Fatal error : Uncaught InvalidArgumentException: JsonMapper::mapClass() requires second argument to be a class name, \Square\Models\Error given. in

I tested all of the code above and it runs successfully. Is that exception happening somewhere else later in your code?

Hi I found it strange too, it should have work.

$date=date(‘Y-m-d’);
$tsdt = $date.“T00:00:00+8:00”;
$today_enddatetime = $date.“T23:59:59+8:00”;

With those line of code I got the output of :

2021-04-16T00:00:00+8:00

When I copy paste the output above into the setStartAt(‘OUTPUT’), it did not work. However, when I manually retype the same exact thing it works. I am a little confused now, is the output different from what I manually typed → 2021-04-16T00:00:00+08:00?

Ok I found my mistake my bad for wasting your time I did not put +08:00

Thanks for the help :smile:

Ha, no worries :slight_smile: glad you figured it out! Thanks for posting the solution as well