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){
$date_parts=(!$date)?preg_split("/[-]+/",$date):preg_split("/[-]+/",date('Y-m-d'));
$year=$date_parts[0];$month=$date_parts[1];$day=$date_parts[2];
$time=mktime(0,0,0,$month,1,$year);
$month_name=date('F',$time);
$days_in_month=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$first_day=date('w',$time);
$calendar='

'.$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}
.highlight{color:#E81490}

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.

    Standard
    code

    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){
    $counter=strlen(strip_tags($str));
    if($counter>$max) return true:return false;
    }

    Standard
    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?

    Standard
    code, technology, wordpress

    Update to Picased the WordPress Picasa Widget

    Picasaed has been updated to version 0.4

    picasa web iconPicasaed works pretty much the same as the Flickr Widget, but doesn’t use tables, because no one likes tables. It is all sorts of XHTML friendly, if that’s what gets your motor running. It doesn’t do anything fancy, it just displays X number of photos from a specific album. If it doesn’t work tell me and I’ll try to fix it.

    Version 0.4

    • Picasaed now uses the thumbnail image (faster loading!)
    • photo titles are used when present
    • photos now link to album
    • images have proper alt tag now
    • optimized code

    Download PicasaedMore Info

    Standard
    code

    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.

    Standard
    code, technology, wordpress

    XAlign, A TinyMCE Fix for WordPress

    It’s bothered me that TinyMCE, WordPress’ default rich text editor, uses the align attribute. For example if you want to center some text and use TinyMCE you’ll get code that looks like this:

    <p align=”center”>My centered text</p>

    Why is that a problem? Here’s the list

    1. It is not valid strict xhtml
    2. It is deprecated transitional xhtml
    3. It does not function as expected across browsers in all cases

    Anyone writing new code should aim to have valid strict xhtml. Why? Because strict code will offer a better experience for everyone across browsers, it will be easier to update, and it is more future-proof (though nothing is 100% future proof). Hopefully WordPress will come up with a built-in solution soon and render this plugin pointless, but until then this works.

    Download

    Standard