Categories
PHP Web Development

Formatting dates in PHP

Formatting dates in PHP can seem a little daunting.  The best way to start is to ensure that you've stored all your dates with the slightly counter-intuitive time() function. This stores the date and time together in your database as one long number that really doesn't mean anything. This is a Unix timestamp.
The date() function is then used to reformat it. For instance 1206371089 comes out to March 24th, 2008 at 15:04:49 (GMT).

If your date/time is stored in the variable $lastupdated, you can display it in a friendlier format as;
[sourcecode language='php']
echo date("Y-m-d H:i:s",$lastupdated);
[/sourcecode]

which will give you a date display like 2008-03-24 13:04:08 (note that this format is well suited to sorting chronologically in the output if it needs to be sorted after processing.)

If you need to refomat a date stored as a string, this is the way to do it;
[sourcecode language='php']
echo date("l jS F Y",strtotime("2008-03-05"));
[/sourcecode]

You can format the dates pretty much however you want.  For a list of the format codes, it's best to go straight to the source at http://www.php.net/date.

Categories
Web Development

Software Tools for Web Developers

Here's a list of invaluable tools for web developers.

  • Notepad++
    • This open-source text editor is free to use. Very powerful, with syntax colouring.
  • grepWin
    • A try-before-you-buy global search and replace tool.
Categories
PHP Web Development

Alternate row background colouring in PHP (with only one line of code)

Alternate row background colouring in PHP is something I've seen and used often, but I was fooling with it on one of my projects today and realized it could be done with one line of code.

First, notice that I've got a while loop to cycle through the records in a dataset I've pulled from a database. You'll also see in the first and last rows that I've created the table outside the WHILE loop because the table is only created once. A row is created once for every row in the dataset so it's inside the FOR loop.

To set the background row I use a ternary operator which tests a condition, then assigns one of two values based on the evaluation of the condition. If you aren't familiar with the Ternary Operator, get to know it. It will dramatically simplifyyour code. Here's a basic example; 

1
$variable = (colour = "green") ? "The colour is green." : "The colour is not green." ;

So looking at the green line, basically what it says is this;

If the variable $bgcolour is not set, or if it is "$F0F7FF", set the value of $bgcolour to "#FFFFFF".

Otherwise, set the value to #F0F7FF". 

Note here that the condition has two parts separated by the OR operator, "||".  The first condition !isset($bgcolour) checks to see if the variable doesn't exist.  This will occur the first iteration and putting it here saves me from having to initialize the variable beforehand.

1
2
3
4
5
6
echo "<table>";
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = (!isset($bgcolour) || $bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";

Keep in mind – if you use this trick twice on the same page, you'll have to initialize the variable to ensure that the first row background is the same on each table.  You want it to be consistent on all tables.  In this situation it's best to initialize the value just above the while() loop as shown here.

 

1
2
3
4
5
6
7
echo "<table>";
$bgcolour = "#FFFFFF"; // initialize it here, outside the while() loop
while ($row = mysql_fetch_array($dataset)) {
  $bgcolour = ($bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  echo "<tr style='background-color:$bgcolour;'><td> TABLE DATA GOES HERE </td></tr>";
} // end while
echo "</table>";
Categories
Web Development

Troubleshooting XHML and CSS

An intractable error hiding in code has driven more than one developer to the brink.

Here are some time-tested tips to help you take control.

  • Copy the page under another filename and work on the copy. This way you can get back to where you started if you need to.
  • Validate your XHTML, checking for unclosed or improperly nested tags
  • Validate the CSS
  • Three out of four times my problem is solved throught the validating process. Seriously, it's that important.
  • Remove all the code between the <body></body> tags
  • Remove all the CSS; external, and embedded (the inline CSS code was removed with the body code).
  • Restore the main layout elements with their CSS code pertaining to layuout (position, alignment, floats, margins, padding.). Test that page as you add each element.
  • Add different colored borders to your main divs to make sure they're what you you intended {border: 1px dashed red;}
  • Add padding and margin rules back in one by one
  • Insert some dummy content (see www.lipsum.com)
  • Add the rest of your content and CSS code back gradually

While you're doing this, forget your mouse. Alt-Tab to switch between editor and browser, Ctrl-S to Save and F5 to refresh your browser will make this process infinitely faster. You need to constantly flip back to the browser and refresh to identify where results change. Don't waste time and break concentration by taking your hands off the keyboard.

Two Firefox plugins will help you immensely when debugging code;

  • CSSViewer – provides an on-demand pop-up view of the CSS properties of whatever you're hovering over
  • Web Developer – Explore all the options of this plug-in. It has a lot to offer.
Categories
Operating Systems

Control Panel Applets – Command Line options

Many Windows users know that control panel applets can be called from the Run line. For example, using the syntax below will call up the Regional and Language Options applet.

control intl.cpl

However, there is more to it than that.

For an applet like main.cpl which serves multiple purposes, you can use a command line parameter – @# – where # is the zero-based number of the tab you want it to open at. For instance, @0 will open the mouse control applet, @1 will open the keyboard control applet.

control main.cpl,@1

If you want to open the applet with a specific tab open, there's another parameter you can use. Use the zero-based number of the tab you want it to open at. For instance, the following run line will open the mouse applet with the third tab (pointer options) open. Remember, it's zero-based, so we use 2 for the third tab.

control main.cpl,@0,2

For further information, there's a great article on this at vlaurie.com.