Hurry and wait
Woke up this morning to an urgent email from Heather saying that her store was down.
I read this *just* after getting out of bed.
While I didn’t fix it, I put a band-aid on it and had the store up and running within an hour(more or less).
Then it kinda slowed down from there.
Got some work on her store done. Found out things that I needed to know.
Then I played around with PHP for a little while and found what could be a bug.
OK, so here’s the thing.
getdate() takes a unix timestamp and parses it out into an array that you can use to get date/time info.
Like the fact that it’s Tuesday or something.
So I was going to do this to track time for working on projects.
Something like a php stopwatch.
I wondered if it worked with small numbers, like 100. See that would just be 1 minute and 40 seconds.
What I got was 19 hours, 1 minute and 40 seconds.
Huh.
Put in 3600(one hour), get back 20 hours.
And it goes on like this until the 24 hour mark, where it goes back to 0.
If I put in a current timestamp, then it spits out the right time/date/etc.
Weird.
There must be something I’m missing. Like it’s in a funky timezone or something.
So, I’ll use normal math to do all this.
Not like it’s hard.
Filed under: Blog - @ January 15, 2008 11:37 pm
Ah, the joys of the web… 🙂
I did a quick check on getdate() and your small values seems to be an issue since 0 = Unix epoch. That’s typically somewhere in 1970 isn’t it? (According to time() it’s Jan 1, 1970…)
Now fair enough if you were supplying a current datestamp along with the int… It’s something else entirely then.
Don’t see an easy way of doing what I think you’re wanting to do… except going a custom library kind of road… Like you say, easy enough to put together though.
REF: http://us.php.net/manual/en/function.getdate.php
Of course, having said that, the default of 0:00:00 should give the desired behaviour… though the docs say time() starts Jan 1, 1970 GMT.
getdate() seems to imply its handling of the epoch is system dependant though.
(So yeah, probably timezones + potential system variation… ?)
There’s some time mangling via the time() docs (in the comments)… Might be of some help…
REF: http://us.php.net/manual/en/function.time.php
How delightfully arbitrary.
Do Unixian creationists have anything entertaining to say about the flood?
gettime() returns a count of the seconds since jan 1, 1970. What you want to do is to just be working with date/timestamps, if you want to do things like record aribitrary points in time, and determine the intervals between said arbitrary points in time.
— Cent, tool, job. If you’re just trying to keep track of hours that you worked or something, for billing purposes, use Excel.
Yes but you’ve missed the point.
Unless Excel has a stop watch feature that I don’t know about, then I can’t use it.
The idea here was to click a button, and the timer would start.
Then I do work.
Then I click another button, and the timer would stop. Then it would tell me how long I worked.
Two pretty much arbitrary points in time, measured in seconds.
Subtract those two points, and you get how many seconds I was working.
If that worked out to 100, then that’s 1 minute and 40 seconds.
If I plug that into a unix timestamp thingy, then it would be Jan 1, 1970, midnight, 1 minute 40 seconds.
But it’s not. And that’s what I’m complaining about.
So no, Excel is not the right tool for this particular job 😛
It happened sometime in the 80’s with PCs….
Some do (http://en.wikipedia.org/wiki/Denial-of-service_attack#ICMP_floods).
Though it’s debatable whether that’s entertaining.
The end of the world should happen in 2038 (http://en.wikipedia.org/wiki/Year_2038_problem) or 2070 (http://en.wikipedia.org/wiki/Year_2070_problem) though…
[Before you speculate that I have too much time on my hands… That was all sadly too familliar and easy to find… though the 2070 thing was new to me…]
Excel will do that quite easily. It’s dirt simple if you’re not too lazy to type in your start date/time and stop date/time, and dirt simple (but takes ten seconds longer) if you want start/stop buttons.
What email address would you like the example Excel sheet I just whipped up sent to?
But no, to do it the way you want, you’d have two variables, call them timeStart and timeStop. Into timeStart you’d put your seconds elapsed since epoch, and into timeStop you’d put your stop time, same manner.
Time worked (in decimal notation of hours) = (timeStop – timeStart) / 60 / 60)
If you wanted hh:mm:ss style, you’d divide timeTotalSeconds / 60 to get the minutes, and timeTotalSeconds mod 60 to get the left over seconds.
Then, you’d divide timeTotalMinutes / 60 to get the total hours, and timeTotalMinutes mod 60 to get the left over minutes, then display timeTotalHours:timeTotalMinutes:timeTotalSeconds.
That having been said, you’d simply be capturing start and stop times into your database, and worrying about the formatting later. And in that case, you’d simply be writing a good old date/time stamp into your start field, and a good old date/time stamp into your stop field, and using an actual date/time function, which, as somebody pointed out, PHP doesn’t do will, to subtract the other from the one.
First thing you learn when you start working with the Gregorian calander and the 24 hour clock; it’s fucked up. It’s a ridiculous bitch to work with once you start worrying about day breaks, leap seconds, leap years, that multi-day stretch back in the middle ages that they just skipped for good times, and so on. Heaven help you if you’re working across timezones.
–Cent, tends to run into this kind of crap when he’s coding billing and, horror of horrors, time tracking systems.
Cool!
But you can keep it 🙂
Besides, it’s not as cool as writing it on my own using PHP and MySQL, having it web accessible from anywhere(yes, I’m sure I could use Google Docs or something) customizing it to the nth degree and generally just keeping me busy in the off time.
But point taken.
You won’t mind if I just stay out of this entirely? Great.
Buuuuuuut….aren’t you in it now? 😛
I delight in contradiction.
Ok.
So, to sum up, you want to use getdate() to get your start time, and your end time. Subtracting start time from end time will give you elapsed time, in seconds. You cannot then give those seconds back to getdate() to display as a friendly string of any sort; you need to do with those seconds as you will.
Generally, that means converting them to straight decimal hours, and multiplying that by your hourly rate.
–Cent, heaven.
Actually, I use two calls to time(), one as the start and one as the end. I subtract these and get the seconds worked.
The problem with getdate() is that it doesn’t seem to want to work properly with fiddly small numbers, so I’ll have to do that the old math way.
All in all, I suppose I could just use a clock, a sticky note, a pen and some basic math skills, but there’s no fun in that either 🙂
getdate() will work fine for anything more than one second. You just need to do your own work to display the seconds elasped as something other than seconds elapsed.
Or, instead of PHP, do it in Perl. They’ve got tons of good date/time libs, including sub-millisecond libraries.