code, technology, tips

Dead Simple PHP Calendar

I needed a calendar for a PHP project I was working on. I did a quick look around and found some promising solutions, but they all seemed way more complex than what I wanted. They also used tables, which are gross. This is a ridiculously easy way to get a calendar from php:function calendar($date=false){

'.$month_name.' '.$year.'
  1. Su
  2. Mo
  3. Tu
  4. We
  5. Th
  6. Fr
  7. Sa
  8. ';
    for($x=0;$x< $first_day;++$x)$calendar.='

  9.  ';
    for($x=1;$x< =$days_in_month;++$x)$calendar.=($x==$day)?'
  10. '.$x.'':'

  11. '.$x.'
  12. ';
    return $calendar.'


How it works
It is actually pretty simple. If you feed the function a date it will make a calendar and highlight that day of the month. If you do not feed the calendar a date it will make a calendar and highlight today’s date.

How to use it
The simplest way to call this function is or if you are feeling romantic you can specify a date You must format the date YYYY-MM-DD (leading zeros are optional).

Making it look nice
Tables are not the worse thing in the world, but they are not even close to the best way to do this. Styling the calendar in CSS is a far better choice, especially if you want to go above and beyond with you calendar. If you just want a basic calendar you can use CSS like this:#calendar{position:absolute;top:6em;left:50em;width:14em;min-height:12.5em;margin:0 auto;text-align:center;font-size:0.6em}
#calendar h6{font-size:1em;margin:0 1em;display:inline}
#cal_body li{list-style:none;width:1em;height:0.8em;float:left;padding:0.2em;margin:0.1em;text-align:center}

Of course we can make this more complex
For example, we could add navigation to get to the next month or previous month. We could also make the dates into links. For my purposes I did both.

Adding monthly navigation can be done by editing one line:$calendar='

< <

'.$month_name.' '.$year.'


  1. Su
  2. Mo
  3. Tu
  4. We
  5. Th
  6. Fr
  7. Sa
  8. '

If you want to use that, you will probably need to edit the link so that it connects to whatever/wherever your calendar is located.

Making the dates into links is also just a simple matter of editing one (long) line:for($x=1;$x< =$days_in_month;++$x)$calendar.=($x==$day)?'

  • '.$x.'':'

  • '.$x.'
  • ';Again, you would need to edit that link so that it connects to everything to the function.


    Count Characters, but Not HTML or Links

    I took a lazy Google look and didn’t see how to do this. If you want to use PHP to count characters in a string but only the visible text (in other words you don’t want to count the html— like links and whatnot), then just use this function. It returns the string length.

    function visible_count($str){
    echo strlen(strip_tags($str));

    Of if you want to reject a string that is too long, try the below. It returns true if the string is within a limit, false if it exceeds the limit. Modify at as needed.

    function the_limit($str,$max){
    if($counter>$max) return true:return false;

    technology, tips, wordpress

    The Perils of Using a Free WordPress Theme

    Take a look at what happened to this blog’s hit counts after switching (briefly) to a new and free WordPress theme downloaded from the official WordPress website.

    Before I used my own variation of the freely available sandbox theme. I switched after updating the template andlosing some of my work on it. Now I’ve gone back with though I haven’t put any real effort into making the site look “good.”

    Since replacing the theme, my site’s stats have returned to normal. I won’t name the theme that killed my blog’s stats for about a week, because that’s mean, but it should serve as a warning. By itself the sandbox theme is fairly SEO friendly, but with with a little tinkering you can get something that knocks the mittens off kittens. Sandbox is the exception though, not the rule. Many of the themes available out there a garbage. It is better to make your own or pay someone with brains to do it for you.

    Remember, free is sometimes good, but not always.

    code, tips

    Hiding PHPSESSID

    I’ve built GlitterPony Magazine’s website so that it is dynamic. All I need to do is put the poems into the database fill in a field or two and I’ve got a functioning website. Of course the site is written in strict xhtml so that I can have a new look to each issue simply through modifying the CSS.

    This saves me a lot of time since I can focus on the design aspects and ignore most of the tedious stuff. In fact I’m completely satisfied with this solution, I’ve even written the rewrite rules so that our URL’s all look pretty and are SEO’ed (Search Engine Optimized). The thing I am not satisfied with is that under certain circumstances a vistor would get a url that has something like ?PHPSESSID=5ab080e5cff5dc130f9bec234b stuck to the end of it. Boy is that ugly. It also hurts our ranking in search engines. But there is a fix and it is very simple.

    All you need to do is add php_flag session.use_trans_sid off to the .htaccess file and bang! You got clean urls. Of course if you’d like you can add ini_set('session.use_trans_sid', false); to your php files, but why add that to all your files when you can add one line to your .htaccess file?


    Generating a Random User Password in PHP

    Creating a custom random password generator in PHP isn’t difficult, for my purposes I often use this simple function

    // $group is the characters we want the password possibly composed of
    // $pass_length is the length we want our password to be
    function random_password($group, $pass_length){
    	for($p=0; $p<$pass_length; ++$p){
    		// pick a random number, based on the time
    		$random_num	= rand(0, ((double) microtime()* 1000000)) % 67;
    		// user the random number to pick a character from $group and add it to the password
    		$pass		= $pass.substr($group, $random_num, 1);
    	return $pass;

    Now to use this function all you need to do is set a group of characters you’d like to allow in your passwords and a password length. You could do something like this

    $password	= random_password('tkmF#w8EcRzW6GvOPfcgNsQD1hArU4Y$Lx2a7Mu0jT3B!q5SyXJCheInpKZbHV9', 10);

    which would give you passwords that are all ten characters long. Or if you want to vary that you could do something like this

    $password	= md5( random_password('abcdefghijklmnopqrstuvwxyz1234567890', rand(8,16)) );

    which would give you passwords that are between eight and sixteen characters long compose of lowercase letters a through z and numbers 0 through 9 AND encrypt it into md5 format which is a good way to store things in a MySQL database.

    Of course, this is perfect for everyone, but it should get you in the right direction. Feel free to use and build on this code. Oh, an of course while this code is freely offered and works just great for me I can’t guarantee it is suitable for all purpose or be held liable for any damages if you somehow manage to become damaged by using this code.


    A PHP Fischer Random Chess Function

    This is of limited use, but I thought I’d share the source code to a function I wrote the other day to setup a game of Fischer Random Chess, also known as Chess 960.

    The basic idea of Fischer Random Chess is that you shuffle up the back row. The rules say that; 1) the bishops must be on opposite colored squares, 2) that the rooks be on opposite sides of the king, and 3) that the black and white side must mirror each other. That’s it. The game is played out like a standard game of chess, though castling is a little tricky.

    The function below outputs an array where the keys are correspond to squares on the board. The array values specify what pieces occupy each square. For example:


    means that a light (white) rook is at square c1. The pieces are noted with the standard letters (r,n,b,q,k). In theory it wouldn’t be hard at all to get it to output in FEN either, though (at the moment) I’m not interested in that personally.

    I’ll be incorparating this into Some Chess, but it could be used in any program or used to setup a real world game too, you just need to work with the array. Check back soon and I’ll share the code used by Some Chess to display an array like the one outputted below as a graphical chessboard.

    function fisher_random(){
    // create an array of every possible square
    $squares = array(0=>’a’,1=>’b’,2=>’c’,3=>’d’,4=>’e’,5=>’f’,6=>’g’,7=>’h’);

    // the get the bishop first
    $tmp = array_rand(array(0=>’a’,2=>’c’,4=>’e’,6=>’g’)); // bishops must be on opposite colors, so only offer one color to each bishop
    $bishop1 = $squares[$tmp];
    unset($squares[$tmp]); // remove the square the bishop got from the array of possible squares

    // get the second bishop
    $tmp = array_rand(array(1=>’b’,3=>’d’,5=>’f’,7=>’h’));
    $bishop2 = $squares[$tmp];

    // place the queen
    $tmp = array_rand($squares); // just use php’s array_rand() function to pick from the array of possible squares
    $queen = $squares[$tmp];

    // place one knight
    $tmp = array_rand($squares);
    $knight1 = $squares[$tmp];

    // place now place the other knight
    $tmp = array_rand($squares);
    $knight2 = $squares[$tmp];

    // take what is left and place the rook, king, and other rook— in that order
    foreach($squares as $tmp){
    if($i==0) $rook1 = $tmp;
    if($i==1) $king = $tmp;
    if($i==2) $rook2 = $tmp;

    // return an array of the pieces, the array keys correspond to the squares and their values to pieces occupying those squares
    return array(


    Here's Some Chess

    Some Chess (Board)Sometime around May 2006 I got this idea to make a Chess server that uses only PHP. There are lots of boring reasons for the PHP part, but the reason for making the server was that I had friends who I played chess with and a bunch of people (including myself) we’re moving all over the place so it would be difficult to keep playing. I don’t like Yahoo, mostly because they help China to imprison people exercising free speech, so I need another option. I couldn’t find anything that fit what I wanted, so I thought, “why not make it.”

    And so I began writing Some Chess, for a while the project took over my life, now it’s more laid back. I’m proud of how well it seems to work and that feel like the program strikes a balance of ease and features while looking pretty nice. Best of all the project is open source, so anyone can install it on their own server for free— modifying it as they see necessary.

    I’ve just released the fourth beta version of Some Chess 2.0, hopefully this will be the last beta needed. From here it is a matter of getting translations, so far I only got Spanish, though I’ve been told there is a German translation on the way.

    If you’d like to see how it works, there is a demo server.

    [tags]chess, chess program, chess server, mysql, open source, php, php chess, some chess, web chess[/tags]