December
4

PHP Date Tips & Tricks

Posted In: PHP, Webmaster by Scott Spear

For those of you using PHP to develop your web pages, I'm sure that you have used the date function at some point. There are so many cases that I have used this to show a date on a page, like using date("Y") in the copyright section of the footer to always reflect the current year. Here are a few tips & tricks to help you save some time.

Formatting

Predefined Constants

This is a trick that I really wish I had known about earlier. PHP 5.1.1 and forward offers date formatting constants. So when I was looking for examples on how to get the date in the correct format when creating my RSS feed, I could have simply used the DATE_RSS constant.

Example

Use:

<? echo date(DATE_RSS); ?>
Output: Thu, 24 Nov 2008 12:29:09 -0500

Instead of:

<? echo date('D, d M Y G:i:s O'); ?>
Output: Thu, 24 Nov 2008 12:29:09 -0500

There are eleven total predefined constants available including DATE_RSS, DATE_ATOM, DATE_COOKIE, and DATE_W3C.

User Defined Constants

Building on this same idea of constants, you could easily create your own date format constants to use. All you would need to do is define a constant with the string value of the date format and then call it within the date function. I have included a simple example below.

Example

<? define(DATE_SCOTT,'m/d/Y'); ?>
<? echo date(DATE_SCOTT); ?>
Output: 11/24/2008

Relative Dates

This trick is one I use when I need to get a date like tomorrow or the day after tomorrow or yesterday. The function strtotime allows you to easily calculate dates like tomorrow and yesterday. You can turn any GNU formatted date string into a PHP date. I have included a couplt of examples below that are based on a publish date of 11-24-2008.

Example

<? echo date('m-d-Y',strtotime('yesterday')); ?>
Output: 11-23-2008

<? echo date('m-d-Y',strtotime('+4 days')); ?>
Output: 11-28-2008

Calculate Age

This trick is a fairly simple and straight forward one. All it does is find an age in years. Simply pass the year(s) you want to compare and it will return the age in year. This could easily be made into a function like getAge($yr1,$yr2);

<? echo floor(abs(strtotime('Y') - strtotime('1985'))/31536000
/*1 year in seconds*/); ?>
Output: 23





December
2

For those of you that are as hooked on phpMyAdmin as I am, you will be happy to know that there is a new version available. phpMyAdmin 3.1.0 includes a number of improvements and fixes.

The list of improvements includes security updates, language updates, export fixes, and general patches. The release notes highlight a few specific improvements: "this version has a new setup mechanism and supports BLOBstreaming and the Swekey hardware authentication" (release notes).

You can view the release notes for full details concerning the new release and download the new version from sourceforge.






November
24

Tablecloth is a small (6 KB) and simple JavaScript file that lets you add some styling and actions to your tables. As you see in the screenshot above, you can highlight the row and column you are looking at. As you hover over the table cells, the rows and columns are highlighted that correspond to the cell you are hovering over. Once you click a cell, Tablecloth will highlight, and keep highlighted, the row and column that make the intersection at the cell selected.

The script is fully customizable through a CSS file (1 KB). There is a default theme, or skin, that comes with the download but can be changed to match your website color scheme. The CSS and JavaScript file together are small and fast loading, adding very little overhead to your page load time.

I did not see any license information available; however, the website does list how to modify the code. This script is written by Alen Grakalic from Css Globe. You can see additional features, view demos, view usage examples, and download Tablecloth from the website.






November
21

Have you ever wondered if your skills are in demand? With so many different possibilities available for web development, it is tough to know if you should keep mastering a specific technology or start learning a new one. After reading an article about how PHP programming jobs are in high demand, I asked myself the follow-up question: what other skills do I have that are in high demand, and which skills do I have that are not?

The article linked to a great resource from oDesk that lists the current (monthly) trends for many of the development and design skills used for web development. There are 84 different trends with a wealth of useful information about the skill's demand including graphs, stats, and recent job postings. Although these stats are pulled from information specifically on oDesk, it represents demand from all around the world. I have listed links to some of the most in-demand skills right now.

As a side note, this site is a fantastic example of how great the Google Chart API is.






November
19

If you have ever wanted/needed to be able to print just part of a page before, now you can. jPrintArea will allow you to print only the contents of a specific element (Ex: div). There aren't a lot of extra bells & whistles that go along with this jQuery plugin, but it works great. It does exactly what is need with only a few lines of code so there is little extra overhead added to your page load time.

There is a sample usage, a demo, and source code (download and view) available from the website. I have included a sample demo below as well.

Demo

User ID Username Email
1 test1 test1@test.com
2 test2 test2@test.com
3 test3 test3@test.com
4 test4 test4@test.com
5 test5 test5@test.com

Print Table






November
15

There are many cases that require a streaming music solution for your website. If you are in need of a free and simple flash mp3 player for your website then you should check out flash-mp3-player.net. This website offers multiple flash mp3 players that may provide you with just what you need. The available players have very few bells and whistles, but they are easy to use, customize, and set up.

There are a total of five different players to choose from: mini, normal, maxi, multi, and js. They all provide customization options like colors and auto play. One of the my favorite features is the built in keyboard shortcuts for controlling playback. You can preview the players, read the documentation, use a generator to create your player, download the player, and view the license agreement for each of the five players. The website states that "[t]he player is under the Creative Commons BY SA and MPL 1.1 license. You can use the player without citing the author" (flash-mp3-player.net).







November
10

If you are in need of a background pattern for your website, PatternCooler is likely to have what you want. There are thousands of backgrounds available for download and are freely available for your use: "All artworks on this site can be used freely on blogs, MySpace profiles, Twitter, mobile phone wallpapers, and non-commercial web projects" (PatternCooler).

The website offers the ability to customize the patterns and backgrounds to your liking using the Seamless Pattern Color Editor (example). This editor, as shown below, allows you to pick a pattern and change the colors using a color picker or hex codes.

Along with the available patterns for editing, there are over 20,000 prebuilt patterns available for download from the User-Colored Seamless Pattern Background Library. Technically you can edit these as well, but they probably have a pattern you like in this collection. Once you have decided on a pattern, you have the option to download and/or view the pattern.

You can get a glimpse of what the backgrounds that are offered will look like simply by viewing the front page. The front page uses one of their patterns as the background and changes with each refresh of the page. Along with the automatic loading of their patterns, you can change the background to whatever pattern you want to see. The view option allows you to see it on as the background on the current page or on a blank page.

Be sure to check out all of the patterns they have to offer. Chances are you will find one, probably multiple, that you like. If you like the service offered, consider donating to help out.






November
6

Sams Teach Yourself books are great resources in a large array of topics. They have released Sams Teach Yourself Ajax, JavaScript, and PHP All in One in paperback version on July 12, 2008. This book is great for webmasters because it includes three of the most popular technologies used on the web.

Like all the rest of the "Sams Teach Yourself" line of books, this one is easy to follow, easy to understand, and takes you from a beginner to an intermediate and beyond. There are a number of visual aids including screenshots and diagrams to help you understand what you have read. Another great help is the amount of code examples used throughout the book; there are examples for most of the topics covered.

The book starts with a general overview at the beginning and works towards a more detailed explanation at the end. In chapter one, you will learn the basics of the web, HTML, CSS, and Ajax. By the end of chapter nine, you will understand JavaScript, using it to manipulate the DOM and add usability to your site. By chapter thirteen you will be using Ajax and by the end of chapter eighteen, you will be using Object Oriented PHP to make your site dynamic. Once you have taken all of that in, you can learn about XML responses, web services (REST and SOAP), and gain a basic understanding about a few popular Ajax tools and libraries that are available.

An added feature with this book is beyond the pages of helpful information. There is a CD provided with the book that includes all of the software needed to get a basic web server up and running. It includes XAMPP, jEdit, Prototype, Scriptaculous, Rico, XOAD, searchable PDF tutorials, and source code for the examples in the book. Most of these are freely available for download on the web, but it is nice to have them all in one place, especially for a beginner that doesn't know what to look for.

This book is well worth your time to read, especially if you are a beginner looking to get not only introduced to these web technologies, but also get started using them. You can pick up your copy at Amazon.com and get started building dynamic websites using Ajax, JavaScript, and PHP.






October
29

Techotopia offers free online books that cover a number of subjects including programming languages, security,  and Linux. The books are hosted on their wiki and are available to read online. The four main books that brought me to this site are:

As the website states that the "IT Essentials series of books are designed to provide detailed information that is accessible to both experienced and novice readers" (Techotopia). The books are clear and concise, providing detailed explanations and examples to help you understand the content. Each book is made up of multiple chapters in a logical and easy to follow order. You can access the table of contents from every page which makes it easy to skip around quickly and easily to look at what you find interesting and useful.

As well as the four books mentioned above, you can find books on these topics:






October
27

The Free HTML Form Template Gallery offers 75+ HTML form templates to fit a wide array of needs. They have form templates for registration, mailing lists, surveys, order forms, and much more. These templates can be downloaded as a zip file or customized to fit a more specific need you may have (requires a free account). The zip files come complete with all CSS, HTML, images, and JavaScript needed to get the forms functioning.

The Free CSS Form Template Gallery offers 50+ CSS form themes freely available for you to use on your site.  All forms can be downloaded as a zip file they way they are or customized to your liking (requires a free account). The zip files come complete with all CSS, HTML, images, and JavaScript needed to get the forms functioning.

All HTML templates and CSS themes are  available under the Creative Commons Attribution license, which means you have the right to copy, distribute, and adapt them as long as you attribute the original work to Wufoo.com. There is a readme file included in each zip file that includes the license terms and conditions.