HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


[RESOLVED] Trying to get timezone adjusted time with {FROM_TIMESTAMP} in template.

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#91693 (In Topic #18425)
Avatar

Community saint

I guess I'm looking for some verification or clarification on the intended behavior of the {FROM_TIMESTAMP} tempcode? I noticed with the YouTube Channel Integration block and with the Twitter Feed block I'm currently working on, the times displayed through the template don't seem to be adjusting to the users timezone and always display the date/time as UTC. Looking at the code in symbols.php, it seems it should be taking the UTC timestamp and converting it to the correct time for the currently logged on user timezone. For example, I have a tweet that was tweeted at 09 September 2012, 02:58:57 AM in my local timezone. The local timezone is America/Chicago which is UTC-06:00 plus daylight savings time so it is actually UTC-05:00. When I pass the UTC timestamp from the Twitter API to {FROM_TIMESTAMP} it converts to and displays 09 September 2012, 07:58:57 AM which is the UTC time. When I was logged on, I was expecting the time to be adjusted to my configured timezone, but it still displays the UTC time. If I'm reading the {FROM_TIMESTAMP} code right in symbols.php, converting to the correct timezone is what the intended behavior is, but it isn't working. I'm not too familiar with dealing with locale specific stuff and all the various conversions for timestamps when it comes to UTC and timezones, so I don't know exactly what the {FROM_TIMESTAMP} code is checking for. What I did was add an echo statement there to see what was being compared and I can see why it's failing but don't know if it's a bug, something with php on my web host, or something else. I added an echo statement like this:

​​

Code

   case 'FROM_TIMESTAMP':
      if (isset($param[0]))
      {
       $timestamp=isset($param[1])?intval($param[1]):time();
       $value=locale_filter(my_strftime($param[0],$timestamp));
       echo "FROM_TIMESTAMP: value = $value / param[0] = $param[0]<br>";  //debug code
       if ($value==$param[0]) $value=date($param[0],utctime_to_usertime($timestamp));
      } else $value=strval(time());
      break;
​​

The echo result was this:

​​

Code


  FROM_TIMESTAMP: value = 09 September 2012, 07:58:57 AM / param[0] = %d %B %Y, %I:%M:%S %p
​​

Passing a UTC timestamp to {$FROM_TIMESTAMP,%d %B %Y\, %I:%M:%S %p,{TWEET_TIMESTAMP}} I get UTC time being displayed (09 September 2012, 07:58:57 AM) instead of the expected timezone adjusted time (09 September 2012, 02:58:57 AM). The if statement fails in the FROM_TIMESTAMP code and therefore the UTC timestamp is never passed to utctime_to_usertime() and converted. In my block code if I pass the UTC timestamp to utctime_to_usertime() first and then pass that to the template I get the correct time displayed, so I know the timestamp is UTC and the utctime_to_usertime() function isn't the problem. Some additional info...I have my logged in account set for America/Chicago (US Central -0600) timezone, the web server is in the America/Los_Angeles (US Pacific -0800) timezone, and the default timezone for the web server is UTC.



Last edit: by Jason Verhagen
Back to the top
 
Posted
Rating:
#91694
Avatar

Thanks. That code wasn't clear at all, and Timezone-bugs are so easy for us Brits to not notice.

Fix…

Code


         case 'FROM_TIMESTAMP':
            if (isset($param[0]))
            {
               $timestamp=isset($param[1])?utctime_to_usertime(intval($param[1])):time();
               $value=locale_filter(my_strftime($param[0],$timestamp));
               if ($value==$param[0]) // If no conversion happened then the syntax must have been for 'date' not 'strftime'
                  $value=date($param[0],$timestamp);
            } else $value=strval(time());
            break;



Become a fan of ocPortal on Facebook or add me as a friend. Add me on on Twitter.
Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about ocPortal whenever you see the opportunity.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying ocPortal on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Back to the top
 
Posted
Rating:
#91695
Avatar

Actually, we'll let Tempcode coders have an extra ,0 on the end of the parameters if timezone conversion is not desired.

Code

         case 'FROM_TIMESTAMP':
            if (isset($param[0]))
            {
               $timestamp=isset($param[1])?intval($param[1]):time();
               if ((!array_key_exists(2,$param)) || ($param[2]=='1')) $timestamp=utctime_to_usertime($timestamp);
               $value=locale_filter(my_strftime($param[0],$timestamp));
               if ($value==$param[0]) // If no conversion happened then the syntax must have been for 'date' not 'strftime'
                  $value=date($param[0],$timestamp);
            } else
            {
               $timestamp=time();
               $value=strval($timestamp);
            }
            break;


Become a fan of ocPortal on Facebook or add me as a friend. Add me on on Twitter.
Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about ocPortal whenever you see the opportunity.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying ocPortal on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Back to the top
 
Posted
Rating:
#91703
Avatar

Community saint

Thanks! I put that code in symbols.php and the times are displaying as expected. :thumbs:

Back to the top
 
There are too many online users to list.
Control functions:

Quick reply   Contract

Your name:
Your message: