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

Time Problems

R

ragtek

Guest
#1
I'm having this code:

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

        $startTime = (mktime(substr($timeString, 0, 2), substr($timeString, 3, 2)) - 1) + XenForo_Locale::getTimeZoneOffset();
        $endTime = (mktime(substr($timeString, 6, 2), substr($timeString, 9, 2)) - 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)
 
R

ragtek

Guest
#3
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
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)
http://www.php.net/manual/en/function.gmmktime.php

thats my 2 cents.

Luc
 

Jaxel

Well-known member
#7
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:

Code:
    public function _preDispatch($action)
    {
        parent::_preDispatch($action);

        $visitor = XenForo_Visitor::getInstance();
        date_default_timezone_set($visitor['timezone']);
    }
 
R

ragtek

Guest
#9
Addition:

I'm using
$visitor = XenForo_Visitor::setup(0);
date_default_timezone_set($visitor['timezone']);

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

Jaxel

Well-known member
#10
You should probably do this instead.... this way you dont have to setup a visitor instance...

date_default_timezone_set(XenForo_Application::get('options')->guestTimeZone);

jaxel at 8wayrun.com

=P
 

xfrocks

Well-known member
#11
I think you guy are doing it wrong. XenForo always stores datetime in UTC/GMT. Proof (in XenForo_Application):

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

ragtek

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

PHP:
        public function cronAction()
        {

            $timeString = XenForo_Application::get('options')->ragtek_plns_time; // 15:35-06:00
            $startTime = (gmmktime(substr($timeString, 0, 2), substr($timeString, 3, 2)) - 1);
            $endTime = (gmmktime(substr($timeString, 6, 2), substr($timeString, 9, 2)) - 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);

                $this->_deleteThreads();
                $this->_closeForum();
            }
            else {
                XenForo_Helper_File::log('nachtsc', 'open  cron  start ' . $startTime . '| now: ' . $now);

                $this->_openForum();
            }
        }
$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%??
 
F

Floris

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

ragtek

Guest
#14
ARGGLLLLLLL

In Options i have set 10:00-13:00
With this code:
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:(
 

xfrocks

Well-known member
#17
ARGGLLLLLLL

In Options i have set 10:00-13:00
With this code:
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:(
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.

PHP:
$startTime = gmmktime(substr($timeString, 0, 2), substr($timeString, 3, 2), 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!