Friday, March 26, 2010

Simple PHP Day Countdown

My fiancée was recently looking at an online countdown until the day we get married. 71 days left as of today! Anyways, it was taking a long time. So I says, "I can do that!" So I made a simple countdown using PHP.

Note: This is for those who already know about either setting up PHP on their own computer or have a server to which they can upload the following code. Check out w3schools for more info on PHP. http://w3schools.com/php/

The basic concept is to find a way to subtract two dates giving you a 'countdown.' This can be done plenty of ways I'm sure, but the easiest for me was to turn both dates into what's called a Unix timestamp. A Unix timestamp is the number of seconds since January 1, 1970.

1. First lets turn our destination date (the date we want to countdown to) into a Unix timestamp by using a PHP function called strtotime().

strtotime("6/5/2010")

This will turn June 5th, 2010 into this: 1275714000.

2. Get the current Unix timestamp for this exact moment in time. This is made easy through the use of the function time()

time()

This exactly as it is above will return the Unix timestamp for that precise moment.

3. Now we can subtract the two amounts of seconds to get the number of seconds between now and our destination date.

strtotime("6/5/2010") - time()

4. In order to get the number of days from that amount of seconds we simply convert those seconds to days by division as follows. Lets say our seconds returned from the above calculation equals 172800. Take that number and divide it by the number of seconds that are in a day, 86400.
I can never remember how many seconds are in a day (I said I was a nerd...but come on). So a simple solution is to divide the seconds that are returned by this: (60 x 60 x 24). 60 seconds multiplied by 60 equals the seconds that are in an hour. Times that by 24 and that gives you the seconds in a day. The two statements below work the same.

$days = (strtotime("6/5/2010") - time())/(60*60*24);
$days = (strtotime("6/5/2010") - time())/86400;

5. Next will be to round this number because it will be returned as a decimal such as 1.78483. So we use the ceil() function to round up.

$days = ceil((strtotime("6/5/2010") - time())/(60*60*24));

6. Then simply display the variable we just filled.

echo $days;

All together now.

$days = ceil((strtotime("6/5/2010") - time())/(60*60*24));
$s='';
if ($days!=1) {
     $s='s';
}
echo $days. " day$s till we get married!";



Note the $s variable is to determine if I should make the word 'day' plural or singular upon writing it to the page. It basically checks to see if the $days variable equals 1 or not. If it does the word 'day' will be written. If not, 'days' will be written. Pretty simple.

Well hope this helps you countdown the days or basically whatever you want to count, until the special date.

Thursday, March 18, 2010

Hiding Facebook Friends

So I recently got a Facebook email from a radio personality from St. Louis asking about an application (Pending Friend Requests) I had written on Facebook.  She asked if the app allowed users to see which friends you add or delete. It does not. It will not distinguish between an Ignore and letting the request sit and wait (pending). Facebook already lets you view pending friends, but it is in a much more convoluted way. The app simply lets you view them in a nice convenient way.

Anyways, this radio personality said a friend was upset with her because she had added other friends but not them. The only way a 'non-friend' can see your friends is by viewing your profile publicly. To insure that 'non-friends' cannot view your friend list follow the steps below.

Note: The Facebook layout changes with every gust of wind, however, the following directions are guaranteed to work with the current Facebook as of the publishing of this post.

1: Once in Facebook click the 'Profile' button at the top of the page.


2. Scroll down to your friend list on the left side of the page and click the pencil icon.




3. Uncheck the box that says, "Show Friend List to everyone."



4. Done! Now you don't have to worry about other people being nosy and or offended by you ignoring them. :)

Oh the good ol' days when a real friendship started with a handshake or an innocent flirt.  Sometimes I wonder if Facebook really messes up what could be great real friendships.

Wednesday, March 17, 2010

Intro

Well, hello there internet traveler. I assume you come looking for answers. Take a look around. I might have what you are looking for. I add a few new tutorials every week. Don't believe what the date on the latest post says.

I am a nerd. You are safe here. I know what its like to be working on a project and either not be able to finish or its taking way too long to finish. So I pledge to write a tutorial for many of the random things I do. I don't know how helpful that will actually be to all of you, but for the maybe half a percent of the world population that shares the same interests as me, I present to you a gold mine.

I wish to have tutorials on things ranging from making a website to putting in a car stereo system to building a custom computer (mac or pc). Basically things that I would need help on. Trust me this is going to be random and sometimes very specific. For example, last week I put Windows 7 on an Asus Eee PC using a jump drive. Got a project like that? Everybody needs a nerd.

Oh yeah, feel free to leave comments suggesting new things I should do tutorials on. It will only help. Thanks and enjoy!