1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Time Problems

Discussion in 'XenForo Development Discussions' started by ragtek, Sep 7, 2011.

  1. ragtek

    ragtek Guest

    I'm having this code:

    $timeString XenForo_Application::get('options')->ragtek_plns_time// STRING "15:35-06:00"

    $startTime = (mktime(substr($timeString02), substr($timeString32)) - 1) + XenForo_Locale::getTimeZoneOffset();
    $endTime = (mktime(substr($timeString62), substr($timeString92)) - 1) + XenForo_Locale::getTimeZoneOffset();

    $now XenForo_Application::$time;
    Sometimes the values are ok and sometimes it's +/-2h

    Isn't the time the same, if i run the script and if the server runs the script (as cronjob)???

    I've lost 7h of my wonderful life with this and it's still not working as it should

    What i want to accieve is:

    start => 20:00
    end => 06:00
    From the options

    and then=>
    get the timestamp for the startdate
    get the timestamp for the enddate

    compare these values with $now

    (edit: it's because of the timezone, right? but how can i prevent this?:()
    can i create a "visitor copy of my user with the set timezone for the cronjob)
  2. ragtek

    ragtek Guest

  3. ragtek

    ragtek Guest

    I'll pay 20€ per paypal if anybody can show me, how this stuff is working and why i'm having different results with the same code...
    I bet it's a very stupid mistake in my code/logic, but i don't want to search anymore:(
  4. Luciano

    Luciano Member

    I am not sure, but trying to put you in the right direction... 2 thoughts.
    1 - At first sight, there may be an inconstistancy.. you take the TimeZoneOffset into consideration when getting start and endtime, but you dont when you get $now
    so you might (not at all sure) have to add the the TimeZoneOffset also to the $now ???

    2 - To get it absolute, I would convert all timestamps to gmt, prior to comparing. (I think you can use gmmktime instead of mktime)
    before you use that, checkout the manual though (cause I might be wrong)

    thats my 2 cents.

  5. Digital Doctor

    Digital Doctor Well-Known Member

    Is it a Daylight Savings Time issue ?
    Sorry. I know it probably isn't.
    Jaxel dealt with this stuff in XenAtendo.
    Ask him.
  6. SheepCow

    SheepCow Well-Known Member

    Have you tried setting the 7th argument to -1 (the $is_dst argument)?
  7. Jaxel

    Jaxel Well-Known Member

    Time is a MAJOR pain in the ass with XenForo and to this point, I still haven't got it working correctly in a lot of cases... However, adding this solved a lot of my problems:

        public function _preDispatch($action)
            $visitor = XenForo_Visitor::getInstance();
    mlx, kkm323 and (deleted member) like this.
  8. ragtek

    ragtek Guest


    yes, that's really a strange way, BUT it's working

    thx very much.

    so paypal adress?
  9. ragtek

    ragtek Guest


    I'm using
    $visitor = XenForo_Visitor::setup(0);

    to be sure that always the default timezone is used (even i run the cron via acp => run cron)
  10. Jaxel

    Jaxel Well-Known Member

    You should probably do this instead.... this way you dont have to setup a visitor instance...


    jaxel at 8wayrun.com

  11. xfrocks

    xfrocks Well-Known Member

    I think you guy are doing it wrong. XenForo always stores datetime in UTC/GMT. Proof (in XenForo_Application):

    self::$time = time();
    So ragtek should use gmmktime instead. Dealing with UTC timestamps everywhere is easy!
  12. ragtek

    ragtek Guest

    It seems that i'm too stupid for the time functions:S

            public function cronAction()

    $timeString XenForo_Application::get('options')->ragtek_plns_time// 15:35-06:00
    $startTime = (gmmktime(substr($timeString02), substr($timeString32)) - 1);
    $endTime = (gmmktime(substr($timeString62), substr($timeString92)) - 1);

    $now XenForo_Application::$time;
    // Overlapping day?
    if ($endTime $startTime) {
    $endTime += 86400;
                if ((
    $now $startTime) OR ($now $endTime)) {
    XenForo_Helper_File::log('nachtsc''close  cron start ' $startTime '| now: ' $now);

                else {
    XenForo_Helper_File::log('nachtsc''open  cron  start ' $startTime '| now: ' $now);

    $startTime & $endTime are ok, but $now is -2h because of UTC
    So, do i really need to add the (guest) timestamp offset to $now and it will work 100%??
  13. Floris

    Floris Guest

    I remember playing with php and timezone stuff, etc, Don't forget to set locale .. otherwise it will keep jumping around.
  14. ragtek

    ragtek Guest


    In Options i have set 10:00-13:00
    With this code:
    $timeString = XenForo_Application::get('options')->ragtek_plns_time; 
                $startTime = (gmmktime(substr($timeString, 0, 2), substr($timeString, 3, 2)) - 1);
                $endTime = (gmmktime(substr($timeString, 6, 2), substr($timeString, 9, 2)) - 1);
                $now = time();
     if ($endTime < $startTime) {
                    $endTime += 86400;
    i'm getting:
    start 1317722442 => 04.10.2011 12:00:42 ( UTC: 04.10.2011 10:00:42 )
    end: 1317729942 => 04.10.2011 14:05:42 ( UTC: 04.10.2011 12:05:42 )
    now: 1317723823 => 04.10.2011 12:23:43 ( UTC: 04.10.2011 10:23:43)

    BUT my start is : 10::00
    and end is 13:00
    I'm really lost with this:(
  15. ragtek

    ragtek Guest

    Yes today.
    But it's throwing an error because it's deprecated:( (and i should use the php5 timezone functions)
  16. Mythotical

    Mythotical Well-Known Member

  17. xfrocks

    xfrocks Well-Known Member

    In your gmmktime call, you have to pass 0 for stuff that you don't use otherwise they will use the current time value for that. Specifically, you may need to set second to 0.

    $startTime gmmktime(substr($timeString02), substr($timeString32), 0);
    Also, I don't understand why you have to subtract 1 for $startTime and $endTime?

    Doing this way you will get the correct $startTime. Not sure why your $endTime is incorrect but are you sure you don't set any spaces in the option? You should double check it.

    Almost there ragtek!

Share This Page