I've posted this same question to several PHP related sites almost a week ago, with absolutely no response. You folks seem to be my "last resort", so to speak.
I've built a "web app" in PHP to handle data and business operations for the cab company I work for, and a part of the application acts as a sort of "alarm clock", displaying pre-arranged pick-ups and wake up calls. Here's the "full story":
The other day, our server was upgraded to a faster computer, and the entire application's infrastructure were upgraded, as well (apache server 2.0 to 2.2.3, php 5.0 to 5.2.5, and mySQL 4.3 to 5.0.25). I've gotten all the usual headaches dealt with (changing query syntax, fixing the "old password" hassle, etc.), except for one, and I'm not sure what to do about it. I'm missing something simple, I just know it. The trouble lies in the following section of code:
Code:
list($h, $m) = split(":", $time);
// hh:mm syntax string, retrieved from the db
$timeOffset = "+$h hours $m minutes";
// $timeOffset is the fine offset (from midnight) to add to the event's current timestamp
$recurring = ($recurring >= 128) ? $recurring - 128 : $recurring;
// $recurring is an 8 bit flag field, denoting wether to repeat the event
// on a later day/days, or if it's a single-use event.
// If $recurring is 128, that denotes a monthly event, which is
// handled later on (and works fine)
$rBin = str_pad(decBin($recurring),7,"0",STR_PAD_LEFT);
//$rBin converts $recurring to a string, representing a 7 bit binary number
$w = date("w", $timestamp);
$c1 = substr($rBin,0,$w);
$c2 = substr($rBin,$w);
$c3 = "$c2$c1";
// the above block determines which day of the week is current, and
// reconstructs the string, starting with the current day
$c = $x = 0;
while ($c == 0) {
$x++;
$c = intval(substr($c3,$x,1));
if ($x >= 7) break;
}
// the above loop counts the number of days till the next event is to occur
$next = "+$x day";
$next .= ($x == 1) ? "" : "s";// if more than one day, make it plural
// $next is the gross offset, in days, to add to the event's current timestamp
break;// Part of an earlier switch statement (not shown)
}
$oldDate = $date;// The event's last stored date
$oldTS = strtotime($date);// The current unix timestamp for the event (midnight)
$oldTS = strtotime($next, $oldTS);// Add the gross offset
$newTimestamp = strtotime($timeOffset, $oldTS);// Add the fine offset
Now here's where it gets interesting. The generated timestamp is accurate about half of the time. But when it's off, it's off by as much as several hours, in either direction (e.g. gets set for 09:15, instead of 05:30, or for 23:30 on the previous day, rather than 06:00 {military time is used for ease of coding}). I have no clue at all what's triggering the inaccuracies.
I'm completely baffled by this behavior, and I'm looking for a more reliable way to handle this. Any suggestions would be humbly and gratefully accepted.
Last edited by mrmortimer on Fri Mar 07, 2008 7:41 am; edited 1 time in total
Joined: Apr 06, 2006 Posts: 2342 Location: Iowa, USA
Posted:
Thu Mar 06, 2008 8:18 am
Sorry, thats an awful lot of code to try and digest. My first thought is "why do all that yourself?". There is this amazing function in php called strtotime() which I use all over the place in GCalendar. It is built on top of a GNU C library which does the same thing. Anyway, it can parse just about any "natural language" string into a time for you, and it handles all the corner cases as well. Maybe you could leverage that in your code.
Only registered users can see links on this board! Get registered or login to the forums!
What it can parse:
Only registered users can see links on this board! Get registered or login to the forums!
You can say things like
$x = some timestamp value
strtotime('+4 hours', $x);
strtotime('-1 month, $x);
strtotime('3 years ago', $x);
strtotime('third monday', $x);
strtotime('next tuesday', $x);
I'm only scratching the surface, it's an amazingly useful function.
Well, after working on trying to state the problem more clearly, I decided to rewrite that section to make it more understandable, and guess what? It now works!
The new and improved code (more of it, actually) follows:
I'm not 100% sure why recoding it with different variable names did the trick, unless I used the wrong variable in the earlier version, and just plain missed it. But at least it works, and that's what matters.
Thanks for helping, Gremmie. Causing me to sharpen my code is what turned the trick.
View next topic View previous topic
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum