I'm trying to display a saved date from a json file. I currently have:

{% set batchRowStats = craft.analytics.loadSavedBatch() %}

{% for stat in batchRowStats %}
        <td>{{ stat['senddate'].date }}</td>
{% endfor %}

And the information in the JSON file is:

[{"batchid":368,"total":1111,"opened":0,"clicked":0,"optout":0,"bounced":0,"subject":"subject line","senddate":{"date":"2017-06-02 06:00:22.000000","timezone_type":3,"timezone":"UTC"}}

I am getting this error:

Impossible to access an attribute ("date") on a string variable ("").

Which I am assuming it's because this information may be in a DateTime or object of sorts. I really need to just display "June 2, 2017" with a date_format but I'm not even sure how to get the string to display.

  • If you {{ dump(stat['senddate']) }}, what does it output? – Brad Bell Jun 20 '17 at 17:24
  • array(3) { ["date"]=> string(26) "2017-06-02 06:00:22.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } – Cyberio Jun 20 '17 at 17:25
  • I was able to {% for datestat in stat['senddate'] %} <td>{{ datestat|date('M j, Y') }}</td> {% endfor %} and it outputs three dates.: Jun 2, 2017 Dec 31, 1969 Jun 20, 2017 – Cyberio Jun 20 '17 at 17:27
  • Seems like that sorts your issue, then? Would you mind adding that as an official answer? – Brad Bell Jun 20 '17 at 17:45
  • Well, it sort of fixes it because I do get a date but why is it outputting three and how do I only get one? I tried .limit(1) and .first() but it gives me errors. Should I try something besides a for loop? If so, what? – Cyberio Jun 20 '17 at 17:49

I attempted to get senate directly and output one element of it but no matter what index/key I set it would give me an error. Also, trying to change the object (or whatever it happened to be) in the PHP caused errors as well. I needed up just doing |first and |date to grab the date I needed and format it. The timezone_type and timezone were being converted into dates (December 1969 and current date) for some reason.

{% set batchRowStats = craft.analytics.loadSavedBatch() %}
{% for stat in batchRowStats %}
    {{ stat['senddate']|first|date("F j, Y") }}
{% endfor %}

I did change on thing in the PHP. I added (array) before the object inside of the array it's in. There are more complications to this scenario but I was assigning it to another variable and adding it to a different array. No matter what way I tried to extract the variable it would give me errors. String conversion gave me array error and array conversion usually gave me a string error and several others along the way. After placing (array) in front of it while assigning it to a variable it seemed to behave a bit better.

$emptyDateArray = (array) $innerResults['BatchStartDate'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.