Thursday, March 29, 2012

PHP: Get Age Comparison Results of 3 Functions

Perhaps I'll blog in detail more about these functions later, but for now, I'm going to present to you 3 of the more popular age formulas and the results I've found.
ALERT: function getAge_2 seems to be the most popular one I've found on the net and is the one that tends to be wrong!
First, the functions
function getAge_1($date) { // Y-m-d format
 $now = explode("-", date('Y-m-d'));
 $dob = explode("-", $date);
 $dif = $now[0] - $dob[0];
 if ($dob[1] > $now[1]) { // birthday month has not hit this year
  $dif -= 1;
 elseif ($dob[1] == $now[1]) { // birthday month is this month, check day
  if ($dob[2] > $now[2]) {
   $dif -= 1;
  elseif ($dob[2] == $now[2]) { // Happy Birthday!
   //$dif = $dif." Happy Birthday!";
 return $dif;

function getAge_2($date) { // Y-m-d format
 return floor((time() - strtotime($date))/(60*60*24*365.2425));

function getAge_3($date) { // Y-m-d format
 return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));

The following results span 100 years from 1/1/1902 to today, 3/29/2012. As you will see in the following two tables, the key difference was found in getting the age of someone with a birthday tomorrow, expect on some years before 1932 where the difference was also found for the day AFTER tomorrow.

The Difference Table






The Full table was way to big, so i'll find another way to show it later, gota get back to the grind, this was just to help others know! Laters!

Monday, March 19, 2012

Helpful Hints: Javascript/jQuery Knowing When the User Stopped Typing

This is just going to be a quick post with something on my mind to help new coders out there. Really I just need a short break from thinking.
Ever seen those handy dandy sites that preform some action AFTER you finish typing, rather than slowing down your device by trying to repeat the action for every character? Wanna know how to do it? GOOD! Then you're in the right place. This "type waiting" is actually very easy! It's all about timing ... or timers rather.
The one flaw to the following system is being unable to know the average type speed of a user. This is often countered by simply using a 2 second timer (although many sites use only a 1 second timer and preform fine). Two seconds is optimal because it is both: an easy amount of time that most people don't mind waiting to see a reaction, and is usually plenty of time to decide if even a "hunt 'n peck" user has stopped typing. However, you will only know what works best for your sight through experimentation. Now, who wants code?
On with the code! First I will example this action in javascript (hang on jQuery users, you're next) as it is really quite simple and will help you get a better idea of what is going on. The first thing you need to do is create a variable outside of your functions (preferably global, but its up to you, how, when, and where you want to use it). It doesn't have to be anything fancy, but be careful of name-spacing as per usual.
Ok, now that the hard part is over let's create our initial function.
Finally the really easy part, your WORK! Simply write the function you want called after the typeing timer stops!
With all the functions made, the rest is too easy. Simply find the element you want to attach it too. And viola! You're all done!
Now, for those of you using jQuery:
Create your variable. Then the code (even easier, cause its jQuery :D)


Monday, March 5, 2012

jQuery: myURL Extension (How to get that pesky site URL on demand/dynamically)

UPDATE: Now includes Parameter support for making site links like

As Seen in jsFiddle Example here:

I would also like to further reiterate that I do appreciate your input, good, bad, or indifferent!
     Let me first say, this is not my usual style blog in that I'm not going to explain this extension much today (perhaps another blog). This is simply to show off a fancy extension/plug-in I've made for jQuery that helps to retrieve your site URL (with parameters!) on demand.

     I find this feature most often useful when doing a lot of Ajax to PHP work where i'm needing to grab specific controllers or controller functions at specific moments. Usually in simple things like loading grids or collecting data for a live change of some sort.

     Anyway, on with the show! I'm going to start this by showing you how easy this function is to use. Then I'll give you all of it's possible uses. Finally, if you're just here for the extension, you'll find it at the bottom of this post.
     The Ease of Use!      OMG! That was so ... easy? Could it really be? And keep in mind, while there are many alternatives out there for doing this, most require you to check the browser version as different browsers have different methods for fetching the key info needed to build your URL string. I already did the work for you!

     Now let's look at many of the possible ways to use this so you can maximize your coding experience without a lot of horrendous thinking (ow my head!).

     Well you saw how to get the basic URL of your sight, but what if you need that ever famous `index.php` page? Try this:
     Now let's try something a little harder. Perhaps you need a image from your image folder that cannot be reached via the index.php but rather by your base URL plus images directory. In other words, your images folder would be like ``. LoL, all too easy for the myURL func!
     Wheh! We're almost to the end (finally, this is taking forever :P). You're final quest is to put it all together and grab a directory with content via the index.php add-in!
     Well, you're almost ready for the actual code, though I should throw just two more piece of info at ya. First, if you really don't like breaking it all down in multiple string parameters, then just use one! See:
     WAIT! What about this parameter update I came here for?      Perhaps you need your site link with data sent to a REST control or a youtube style player is on your site using your own videos, or whatever your purpose! Now simply include a JSON style object as the LAST parameter in your call to myURL and watch the magic happen!
     And finally, for you control freaks out there. I did ensure to include a config that can be run just after you add the plugin. These config options will allow you to set a main directory and sub-directory for both local and on-line use. Keep in mind though, these directories are added before index.php because this is ideal if you are working from a local host with multiple domain directories or even an online sand-box of the same setup. You can also change this at anytime, but every call you make to myURL after you change the config will be reflected.
FINALLY, the Plug-In!