31.10.2009

Lately, a fellow web developer of mine asked me why can’t he manage to save the current time in a MySQL table. The problem was that he didn’t understand how the PHP timing mechanism works. That’s why I decided to write this article.

Working with PHP is easier than most people originally think. It is after all the general purpose scripting language used in web development. Once you understand the basics like time managment and databases, you can very easly produce dynamic web paging using it. Before you even consider setting up a webpage, it must be a priority to understand the basics of PHP. Hopefully you will find the following helpful and informative.

time() and date()

time()

The time() function receives no parameters and returns the current time measured in the number of seconds since January 1 1970 00:00:00 GMT.
This may sound a bit weird but I find it to be clever.

Because one doesn’t want to display dates and times in PHP as a very long number (e.g. “The current time is 1256702873″), the date() function was created.

date()

The date() function returns a string formatted according to the given format parameter.

The function is documented as follows:
string date ( string $format [, int $timestamp ] )
While I prefer to think of it as:
string date ( string $format [, int $timestamp=time()] )
If you don’t pass the second parameter, it uses the time() function instead.

Lets see a couple of examples:
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>

I don’t want to get into the format parameter in this post, but you can read all about it at the PHP manual.

Saving the time in a MySQL Database

The wrong way

My friend was using some sort of a special “date”/”time”/”timestamp”/”datetime” column type because he didn’t know what he was getting from the time() function.
This is not the way to store the result from time(). While it may be useful in certain circumstances, I’d rather save the number of seconds since January 1 1970 00:00:00 GMT for later easier manipulation with PHP.

The right way

All you need to do is use a simple INT or BIGINT column type to store what you get from your time() call.
In order to display it, use the date() function, passing it as the second parameter.

Search terms:

I hate those small evil graphics

If you want to put an end to those annoying smilies that replace your text in posts & comments, follow the next instructions.

First, what are smilies?

Smileys, also known as “emoticons”, are little faces that help you display various emotions. Text smileys are created by typing two or more punctuation marks like :) and :-(

Here is the full list of smilies in WordPress

icon text text full text icon full text
smile :) :-) :smile: lol :lol:
biggrin :D :-D :grin: redface :oops:
sad :( :-( :sad: cry :cry:
surprised :o :-o :eek: evil :evil:
eek 8O 8-O :shock: twisted :twisted:
confused :? :-? :???: rolleyes :roll:
cool 8) 8-) :cool: exclaim :!:
mad :x :-x :mad: question :?:
razz :P :-P :razz: idea :idea:
neutral :| :-| :neutral: arrow :arrow:
wink ;) ;-) :wink: mrgreen :mrgreen:

How do I get rid of them?

It’s actually pretty easy. All you got to do is go to your admin panel -> Settings -> Writing where you will see the following checkbox:
Convert emoticons like :-) and :-P to graphics on display
Uncheck the check box that says “Convert emoticons like :-) and :-P to graphics on display” just like in the image above and click “Save Changes”.

How to replace them

If you want to keep the smilies but you don’t like the current set of smilies you can change them.
Open your FTP program (I use FlashFXP) and upload (overwrite) your custom smilies to /wp-includes/images/smilies.

Looking for more information? Check out the WordPress codex page on smilies.
What about you, do you like them? I just prefer the plain text, it makes it easier to understand. :P

Search terms: