ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Comments and timestamps and confusion...

February 28, 2008 3:38pm

Subscribe [3]
  • #1 / Feb 28, 2008 3:38pm

    Pete Smith

    91 posts

    The confusion is on my part… 😊

    I’m trying to understand timestamps on comments. We want to do a quick report on how many comments were posted during some user-specified time frame.

    So I post a comment on our site. Not as a logged in user. On the user-end of things, the comment is time stamped for 2/28 2:03 PM, which is what my local time was when I posted. So far so good.

    In the database, the comment_date field holds the unix time of 1204247009 and the edit_date holds 2008-02-28 13:02:35. I’m assuming this 1 hour difference is a daylight savings time error, or maybe a local time vs server time? (We’re with enginehosting…maybe someone knows the local time of their servers?)

    But what really confuddles me is that the unix time converts to Fri, 29 Feb 2008 01:03:29 GMT. That’s an 11 hour offset. How’s that making sense? Any ideas?

    My problem is I’m passing in a date like 02-28-2008, and breaking it up and using mktime to convert to unixtime and using that unixtime in my sql query to compare to comment_date, but I’m not getting accurate results.

    Here’s the relevant chunk of my template, and I have PHP set to parsed on Input: (the start/end time are being passed in segment_3 & segment_4, which ends up $urlsegs[4] & [5] when using $_SERVER[“PHP_SELF”];

    <?php 
    $thisurl = $_SERVER["PHP_SELF"];
    $urlsegs = explode("/",$thisurl);
    $startdate = explode("-",$urlsegs[4]);
    $unixstart = mktime(0,0,0,$startdate[0],$startdate[1],$startdate[2]);
    echo "
    Unixstart:" . $unixstart . "
    ";
    $enddate = explode("-",$urlsegs[5]);
    $unixend = mktime(0,0,0,$enddate[0],$enddate[1],$enddate[2]);
    echo "
    Unixend:" . $unixend . "
    ";
    ?>
    
    {if segment_4 != ""}
    <hr>
    
    {exp:query sql="SELECT count(*) AS totalcount
    FROM exp_comments 
    WHERE comment_date > <?php echo $unixstart; ?> AND comment_date < <?php echo $unixend; ?>
    "}
    {totalcount} Comments between {segment_3} - {segment_4} )
    {/exp:query}
    .
    .
    .
  • #2 / Feb 28, 2008 3:48pm

    Lisa Wess

    20502 posts

    Hi, Pete.  You might like to review this kb entry and get your times sorted out before delving into the customization.

  • #3 / Feb 28, 2008 4:07pm

    Matt Stein

    110 posts

    I’m trying to understand something similar. I use FROM_UNIXTIME() in an SQL query using the {exp:query} module. The query result gives me a predictable GMT time. The date and time, including AM/PM, always come back correctly from the query. When I insert the result in the EE template, the time is correct as long as it is AM. If the time stored in the mySQL table is PM, the query result is correct but the template display gets exactly one day added to it.

    For example:
    target/original date: January 1, 2008 11:59am
    mySQL query using FROM_UNIXTIME: January 1, 2008 11:59am
    display on template using {custom_date_field}: January 1, 2008 11:59am

    target/original date: January 1, 2008 12:00pm
    mySQL query using FROM_UNIXTIME: January 1, 2008 12:00pm
    display on template using {custom_date_field}: January 2, 2008 12:00pm

    Relevant Snippets:

    {exp:query sql="SELECT title, url_title, FROM_UNIXTIME(field_id_19+14400, '%M %e %h:%i %p') as exhibition_begin, FROM_UNIXTIME(field_id_20+14400, '%M %e') as exhibition_end, field_id_22 as exhibition_brief FROM db35443_ee.exp_weblog_titles LEFT JOIN db35443_ee.exp_weblog_data ON exp_weblog_titles.entry_id = exp_weblog_data.entry_id WHERE exp_weblog_data.weblog_id = 4 AND exp_weblog_data.field_id_19 <= UNIX_TIMESTAMP(CURDATE()) AND exp_weblog_data.field_id_20 >= UNIX_TIMESTAMP(CURDATE()) AND status != 'closed' AND entry_date <= UNIX_TIMESTAMP(CURDATE()) AND ( expiration_date = 0 OR expiration_date - UNIX_TIMESTAMP(CURDATE()) >= 0 ) ORDER BY field_id_19 ASC"}
    
    
    

    Note:
    I’m formatting the date within the query because doing it in the template ({exhibition_begin format=""}) produced really random dates that I couldn’t find a pattern with. The formatting happened properly, but the dates and times were off by seemingly random amounts of time.

    If it matters, I’m using EE 1.6.2 with mySQL 4.1.11.

  • #4 / Feb 28, 2008 4:21pm

    Pete Smith

    91 posts

    Hi Lisa, thanks for the reply.

    I read that link, and also this one: http://expressionengine.com/wiki/Dates_Explained/

    And in fact added the code from the latter to my template. And I moved everything to our dev server so I can see the time on the server.

    I post a comment at 3:11 local time.

    From a unix prompt, the server says it is Feb 28 14:11:32 EST 2008

    The code from that webpage reports a PHP date of 28Feb08 02:11

    It reports PHP gmdate as 28Feb08 07:11

    In the Comments database the unixtime for comment_date is 1204243913, the edit_date is 2008-02-28 14:11:53

    But again, that unixtime converts to Fri, 29 Feb 2008 00:11:53 GMT.

    I’m trying to understand why php reports the time to be 7:11 today and the date going into the comments title is 00:11 tommorrow.

    I feel like there’s something obvious that I’m missing… time stuff gets my head all spun around.

  • #5 / Feb 28, 2008 4:24pm

    Lisa Wess

    20502 posts

    Hi, Matt - welcome to the forums. Please do read that KB entry and check all the localization options. I’d recommend starting your own thread on this, as localization can be a bit confusing (outside of EE, as well) and trying to track two people in one thread with this issue is not the best option.

    Pete - Well, moving the site around could confuse the issue, since your dev server may have different settings.

    When you put a formatted {current_time} in your format, what do you get back? I suspect that you just have some mismatches with your localization, the system localization, and offsets. Only some experimenting with the various combinations is going to be able to get you working the way you expect.

  • #6 / Feb 28, 2008 4:40pm

    Pete Smith

    91 posts

    OK, still on the dev server 😊 {current_time} gives me 1204245018 which converts to Fri, 29 Feb 2008 00:30:18 GMT.  Which is 5 hours ahead of server time GMT.

    Now we’re getting somewhere! So the entry_date is using the same time as {current_time}, which is wrong.

    So how do I fix that? In Admin>System Preferences>Localization Settings the server time zone is set to UTC - 5.00 (Eastern), Server Offset is blank, DST set to No and Honor DST set to Yes.

    What else should I be looking at?

  • #7 / Feb 28, 2008 4:42pm

    Lisa Wess

    20502 posts

    What if you set your server time zone to no 0 (GMT)?  That 5 hour difference seems to be the issue, no?

    Also, what is your account’s localization?

  • #8 / Feb 28, 2008 5:15pm

    Pete Smith

    91 posts

    OK I set the server to 0 GMT.

    Posted a comment.

    Unix timestamp in the comment database is 1204247423, which is Fri, 29 Feb 2008 01:10:23 GMT.

    So still 5 hours into the future…

    My account (which really shouldn’t factor since 99% of the comments on our site are from unregistered users) is set to GMT - 5.

  • #9 / Feb 28, 2008 5:19pm

    Lisa Wess

    20502 posts

    The comment time stamp will show in the localization of the viewer, so your localization could very well matter.

  • #10 / Feb 28, 2008 5:20pm

    Pete Smith

    91 posts

    How could it if I’m looking at the raw data out of the database and the person posting the comment isn’t an EE user? *scratches head*

  • #11 / Feb 28, 2008 5:22pm

    Lisa Wess

    20502 posts

    Ah, ok, I misunderstood Pete.  Or misread anyway.  What happens when you view that comment’s timestamp on your site?

  • #12 / Feb 28, 2008 5:26pm

    Pete Smith

    91 posts

    The timestamp on the site, from the point of view of a user, is correct. It changes as I change the server time zone, as it should.

  • #13 / Feb 28, 2008 5:28pm

    Lisa Wess

    20502 posts

    Ok,  so everything is working correctly as far as EE is concerned at this point.  Can you back up, now that we know EE itself is doing things correctly,and describe the problem that you’re experiencing?

    Sorry to be difficult, Pete, localization can be pretty confusing to work with. We’ll get there. =)

  • #14 / Feb 28, 2008 5:37pm

    Pete Smith

    91 posts

    You’re not being at all difficult… in fact I appreciate your patience.

    OK so backing up, the problem is that the managers want a count of the number of comments posted within a given time frame. (They’re big on analytics and tracking things like this.)

    The only way I know how to do that is to use a database query and compare user provided start and end dates to the comment_date field in the exp_comments table.

    I let them pass in start and end date in the url in human readable format (ie 02-28-2008) which I convert to unixtime and then compare to the comment_date field via a SQL query.

    But as we’ve seen, that comment_date has offset numbers in it, so the numbers I’m reporting don’t match the numbers showing on the website.

    Honestly, if the comment_date is consistently 5 hours head of the GMT time when they actually posted, I can adjust for that. Any idea if that’s consistent?

    Thanks for working through this with me!

  • #15 / Feb 28, 2008 5:41pm

    Lisa Wess

    20502 posts

    I’m going to have to check with this. But have you considered using the localization class to help you along?  ExpressionEngine does make the localization class available to help you with these issues.  Since EE is getting it right, perhaps using EE’s class with your time conversions can help?  Docs here for the localization class.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases