PHP Date Tips & Tricks
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
Does my website really need a feed?
Whenever a new technology comes out, there is always a lag for adoption and implementation. In my case, when RSS/Atom feeds came out, I too did not adopt them immediately. Before Google Reader came along, I didn't use feeds, let alone implement them into my own sites. After researching what an RSS feed really was, I realized that I didn't only have to learn RSS, I needed to learn XML. I continued to put it off until I realized exactly how much information I obtained through Google Reader, I thought: "I haven't actually viewed most of these websites in several weeks or maybe longer.” This was the beginning of my crash course in RSS and XML.
When you really take a look at how much information is published through feeds, you can see the need for implementing them into your own site. I, as well as many others, I presume, no longer go searching on the internet for news and website updates; I wait for them to come to me through feeds. As a Christian webmaster for a local church, my intent for having a feed isn't to drive visitors to the site in hopes to sell more adds; I am not saying that blogging for business is wrong. I just see feeds related to church and ministry’s sites as a way to keep the church community engaged and aware of what is happening in the church. The ultimate goal of an online presence is to keep people connected outside of the church walls; RSS feeds are a great way to help.
The number of sites using feeds to keep their visitors up-to-date and connected with their site is growing exponentially. FeedBurner, for example, claims that it tracks 934,797 publishers of 1,657,885 feeds. That's right; they track over a million and a half feeds. That is just one site! There are other websites like FeedBurner that have statistics that are just as incredible.
The need for RSS/Atom feeds definitely exists. It is highly recommended that you take the initiative to implement feeds to keep your visitors connected. I have listed some helpful resources below. As always, comments are encouraged.