PHP – Return first n words from a string

Do you ever find yourself needing to shorten a string in PHP? Maybe return the first 25 words of a long story? Give this routine a try. It will return the first n words from a string, or the entire string if it is less than n words long.

Read the rest of this entry »

nextSaturday and lastSunday functions for PHP

I needed a way to find the first second of a week and the last second of a week based on a Unix timestamp.

Credit for the previous Sunday goes to Shaun who posted a way to find it on the PHP site for strftime, although I changed his code a little because I'm more comfortable with the date() function.

PHP:
  1. $lastSunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));
  2. $nextSaturday = mktime(23, 59, 59, date('m', $end_date), date('d', $end_date), date('Y', $end_date)) + ((date('w', $end_date)==6) ? 0 : 86400 * (6 - date('w', $end_date)));

How to add a timer to Eventum

I've been using Eventum for a while to keep track of bugs in a web application of mine. There was one feature missing that I really needed, and which made me switch to dotProject for a few months and that was a timer. When I'm working on a bug I wanted a way to be able to start and stop a timer to keep track of how much time I'm spending.

Read the rest of this entry »

Prototype returning status code 0 in Opera

Seems like browsers don't handle Ajax calls returning a 204 no content status code very well. I've had issues with IE7 gets a 1223 status code instead of the 204. Well, it looks like Opera, at least 9.5.1, gets 0 as the status code.

I'm using Prototype version 1.6.0.3 to handle the Ajax call so I've got onSuccess, onFailure, on204, etc handlers setup along with an on1223 handler for the IE7 issue. Since I've already done that workaround, this one was pretty easy. I just added an on0 handler.

PHP:
  1. new Ajax.Request(url,
  2. {
  3. // Unimportant stuff...
  4. on204: function(r)
  5. {
  6. // Handle the 204
  7. },
  8. on0: function(r)
  9. {
  10. r.request.options.on204();
  11. }
  12. // Unimportant stuff...
  13. })

There's bound to be a better way, but this works for now.

Holiday Cheer on Forums

Seems like Google has made it popular to change the logo of a website for holidays

My question is how many of y’all do the same, and more importantly do your members like it? 

Over on my photography forum I switched out the default logo with a winter themed one for Christmas.  It’s scheduled to last for a couple more days before getting swapped for New Years.  And then I’ve got a couple more scheduled to appear.  But rather than describing them, here are the logos.

Read the rest of this entry »