XF 2.2 Date_from_format and timezones?

Dannymh

Active member
Hi,

So I am working with timestamps for most of the dates on my addon. They all work great programattically in PHP but when I have them in the template and I want to display them from a custom field I used
{{ date_time(date_from_format('U', $thread.custom_fields.EndTimeStamp)) }} Which correctly brings back the GMT time, however I want these to display in the local time or timezone relevant time.

i.e. timestamp is showing as thursday 8th january, 9am. which is in my timezone thursday 8th january 8PM (I dont think thursday was the 8th, thats just a random example.)

Is there anyway for me to have this working with timezone?
 
I tried the following in the ever loving hope it would work

<dd>{{ date(date_from_format('U', $thread.custom_fields.EndTimeStamp, $xf.visitor.timezone), 'D, d, M y, gA') }}</dd>

but it still returns as 9am
 
Still can't get this to work, it looks like if I am reading this correctly templater is not picking this up. I am theorising that this is because I am using a template modification to try and action this and therefore its somehow not parsing things to the templater correctly.

My template modification applies this macro

PHP:
<xf:macro name="custom_field_date_stamp"
	arg-date="!" arg-timezone="!">
	{{ date(date_from_format('U', $date, $timezone,  'D d M Y gA') }}	
</xf:macro>

I have printed out the timezone which is coming through correctly and I have tried hardcoding that into the macro in the template modification to see if I get the result I am after.

It still just defaults back to UTC and never seems to apply the timezone.

The templater method that activates this is

PHP:
public function fnDateFromFormat($templater, &$escape, $format, $dateString, $timeZone = null)
	{
		return \DateTime::createFromFormat($format, $dateString, $timeZone === null
			? $this->language->getTimezone()
			: new \DateTimeZone($timeZone));
	}

If I am reading that right it looks if a timezone is set in the method, if it is null then it gets the default otherwise apply the timezone.

Am I just calling this function incorrectly
 
Finally figured this out and the simple solution was actually to trim() the $date variable, once this was done, it was able to apply correctly
 
Back
Top Bottom