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

Year

getAge_1

getAge_2

getAge_3

*1902-03-30*109110109
*1903-03-30*108109108
*1903-03-31*108109108
*1904-03-30*107108107
*1905-03-30*106107106
*1906-03-30*105106105
*1907-03-30*104105104
*1907-03-31*104105104
*1908-03-30*103104103
*1909-03-30*102103102
*1910-03-30*101102101
*1911-03-30*100101100
*1911-03-31*100101100
*1912-03-30*9910099
*1913-03-30*989998
*1914-03-30*979897
*1915-03-30*969796
*1915-03-31*969796
*1916-03-30*959695
*1917-03-30*949594
*1918-03-30*939493
*1919-03-30*929392
*1919-03-31*929392
*1920-03-30*919291
*1921-03-30*909190
*1922-03-30*899089
*1923-03-30*888988
*1923-03-31*888988
*1924-03-30*878887
*1925-03-30*868786
*1926-03-30*858685
*1927-03-30*848584
*1927-03-31*848584
*1928-03-30*838483
*1929-03-30*828382
*1930-03-30*818281
*1931-03-30*808180
*1931-03-31*808180
*1932-03-30*798079
*1933-03-30*787978
*1934-03-30*777877
*1935-03-30*767776
*1935-03-31*767776
*1936-03-30*757675
*1937-03-30*747574
*1938-03-30*737473
*1939-03-30*727372
*1940-03-30*717271
*1941-03-30*707170
*1942-03-30*697069
*1943-03-30*686968
*1944-03-30*676867
*1945-03-30*666766
*1946-03-30*656665
*1947-03-30*646564
*1948-03-30*636463
*1949-03-30*626362
*1950-03-30*616261
*1951-03-30*606160
*1952-03-30*596059
*1953-03-30*585958
*1954-03-30*575857
*1955-03-30*565756
*1956-03-30*555655
*1957-03-30*545554
*1958-03-30*535453
*1959-03-30*525352
*1960-03-30*515251
*1961-03-30*505150
*1962-03-30*495049
*1963-03-30*484948
*1964-03-30*474847
*1965-03-30*464746
*1966-03-30*454645
*1967-03-30*444544
*1968-03-30*434443
*1969-03-30*424342
*1970-03-30*414241
*1971-03-30*404140
*1973-03-30*383938
*1974-03-30*373837
*1975-03-30*363736
*1977-03-30*343534
*1978-03-30*333433
*1979-03-30*323332
*1981-03-30*303130
*1982-03-30*293029
*1983-03-30*282928
*1985-03-30*262726
*1986-03-30*252625
*1987-03-30*242524
*1989-03-30*222322
*1990-03-30*212221
*1991-03-30*202120
*1993-03-30*181918
*1994-03-30*171817
*1995-03-30*161716
*1997-03-30*141514
*1998-03-30*131413
*1999-03-30*121312
*2001-03-30*101110
*2002-03-30*9109
*2003-03-30*898
*2005-03-30*676
*2006-03-30*565
*2007-03-30*454
*2010-03-30*121
*2011-03-30*010

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)

Enjoy!

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 http://www.mysite.com?para1=test&para2=test+2

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 `http://spyk3lc.blogspot.com/images/`. 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!