Creating an array of date time values via PHP
Thursday, October 22. 2009
I was working on my Reference Statistics Tracker for work, and ran in to a snag.
I am using eZ Components' ezGraph class to create image-based graphs on the fly. It's fast, efficient, and it looks nice - it can even render in SVG (I'll be using JPG or PNG...thanks IE). For my particular needs, I wanted the exported dataset to have a specific formatting to the keys, to represent the different values in my graphs. For instance, let's say I have the following stats from 100 users in our company, and the Operating System they use. This is purely fictitious:
So now I just had to pull the values from the database in the format I wanted. Well my keys in this case were dates, or times (span of days, or hours in a day, or month in a year, etc...). The values were still numeric values in this case. So, we're looking at something like:
The PHP function of range() just didn't cut it. Although I could have converted the datetime values to timestamp values (either via SQL or PHP), and used the 3rd parameter of the function to add the specific number of seconds needed, I was weary of doing that as I remember reading about issues that could cause (adding seconds to a timestamp value calculate future time). Besides, I'd still have to use array_walk() or array_map() to format my array's keys the way I wanted; that left yet another function, and a line of code for the call to array_walk/map. (My host does not yet have PHP 5.3.x, which will allow for lambda functions in array_map.) So, I decided to create my own little function, and figured I'd post it here in case (1)someone could use it and it'd help them out, or (2)someone thinks there's a more efficient way of doing this and wants to give feedback.
This could be useful for similar graphing/charting code, I don't think it's specific to eZ Components'. Perhaps it could be useful for something else that I can't think of. Have fun with it.
I am using eZ Components' ezGraph class to create image-based graphs on the fly. It's fast, efficient, and it looks nice - it can even render in SVG (I'll be using JPG or PNG...thanks IE). For my particular needs, I wanted the exported dataset to have a specific formatting to the keys, to represent the different values in my graphs. For instance, let's say I have the following stats from 100 users in our company, and the Operating System they use. This is purely fictitious:
array('Windows' => 90, 'OSX' => 6, 'Other' => 4);That specific array is crafted perfectly in the format that works well with eZ Components' Graphing class, for a simple bar chart, or a pie chart.So now I just had to pull the values from the database in the format I wanted. Well my keys in this case were dates, or times (span of days, or hours in a day, or month in a year, etc...). The values were still numeric values in this case. So, we're looking at something like:
array('HH:MM:SS' => 10);However, no matter how hard I tried, if there were no values inserted in to the database for a specific time, I could not return a value of null, or zero - even with an outer join. My next thought was to use something like array_merge() on the resulting dataset to fix it.The PHP function of range() just didn't cut it. Although I could have converted the datetime values to timestamp values (either via SQL or PHP), and used the 3rd parameter of the function to add the specific number of seconds needed, I was weary of doing that as I remember reading about issues that could cause (adding seconds to a timestamp value calculate future time). Besides, I'd still have to use array_walk() or array_map() to format my array's keys the way I wanted; that left yet another function, and a line of code for the call to array_walk/map. (My host does not yet have PHP 5.3.x, which will allow for lambda functions in array_map.) So, I decided to create my own little function, and figured I'd post it here in case (1)someone could use it and it'd help them out, or (2)someone thinks there's a more efficient way of doing this and wants to give feedback.
This could be useful for similar graphing/charting code, I don't think it's specific to eZ Components'. Perhaps it could be useful for something else that I can't think of. Have fun with it.
Reference Stats: Client "done"
Thursday, October 1. 2009
The Adobe AIR portion of the Reference Statistics Tracker is "done" (and has been for a week or two now). I just got approval over the extremely basic wireframe layout of the backend (web-based) tool that actually shows the stats data. Although it's basically only storing a very small amount of data, there are so many different ways that the data itself could be shown. If you think about how Web Analytics software can show so many different things; also think about what stats you have with it...it's basically a huge (textual) database of single line entries - the address, the referrer, the time, the visitor's IP, and a response code...yet there are tons of different ways to show that data.
It's a simplistic tool with huge possibilities. I aim to be rather simple for the first iteration out of the door.
Speaking of the first iteration out of the door - I ended up copying/pasting a LOT of the internal code in the Adobe AIR client to finish it. Having a local backup database in case of internet connection failure was a "last minute" additional "requirement" that I hadn't anticipated in the planning process, so I had to hack a lot of ugly stuff in to the code - and to finish it without a lot of it being rewritten, well... Thankfully, I do intend to upgrade it, as I left out one (actually two) important features.
1. Remote upgrade.
(2. Specify end-points to receive dynamic setup info.)
I left out the upgrade script because (1)I've heard it's a resource hog, and is a pain to work with, and...(2)I know I'll need to manually upgrade our library's PCs at the next version because of DeepFreeze anyway. (I left out the portion of the settings because I'm not yet ready to release the code as open source just yet, so I can hard code that stuff for now.)
I will post pictures of both the client and the web tool after it's been in use for a couple weeks - I'd gather that'll probably be about a month from now (I can't start work on the web panel until Monday, and it'll probably take me about two weeks to finish).
It's a simplistic tool with huge possibilities. I aim to be rather simple for the first iteration out of the door.
Speaking of the first iteration out of the door - I ended up copying/pasting a LOT of the internal code in the Adobe AIR client to finish it. Having a local backup database in case of internet connection failure was a "last minute" additional "requirement" that I hadn't anticipated in the planning process, so I had to hack a lot of ugly stuff in to the code - and to finish it without a lot of it being rewritten, well... Thankfully, I do intend to upgrade it, as I left out one (actually two) important features.
1. Remote upgrade.
(2. Specify end-points to receive dynamic setup info.)
I left out the upgrade script because (1)I've heard it's a resource hog, and is a pain to work with, and...(2)I know I'll need to manually upgrade our library's PCs at the next version because of DeepFreeze anyway. (I left out the portion of the settings because I'm not yet ready to release the code as open source just yet, so I can hard code that stuff for now.)
I will post pictures of both the client and the web tool after it's been in use for a couple weeks - I'd gather that'll probably be about a month from now (I can't start work on the web panel until Monday, and it'll probably take me about two weeks to finish).
