Php dynamic year

Filed under: Web Development — rasim

dynamicyearIt is pretty easy to insert dynamic year or date in a php page. I don’t understand the developers that hard-code the years, especially on e-commerce websites. I had to redo a few of them. A lot of times it would be a long list of years without even a loop. I’ve seen over 100-line code listing years and it actually take a lot more work to create a list than write a for loop.

These are some of the places where I think years should be dynamic:

1) Dynamic Copyright Year. Do you like updating your copyright in the footer every year? Do you ever forget? Solutions is very simple:

copyright &copy; <?php echo date('Y'); ?>  yoursiteurl.com

2) Date of birth – dynamic year. This will print years of birth from 5 to 100 year old.


//From hundred years ago
$start_year = date('Y')-100;
//To five years ago
$end_year = date('Y')-5;
//loop through the years, low to high.
for ($i=$start_year; $i<=$end_year; $i++) {
//your logic here, for now we'll just echo it out
echo $i."<br/>";
}

3) Credit card expiration, dynamic year. If you don’t do this, you may get a call a few years after finishing up the project. Just kidding, but it sucks to change hard-coded values when customers call and complain that they can’t check out because of the credit card expiration year.

//From current year
$start_year = date('Y');
//To a few years in the future, ten in this case
$end_year = date('Y')+10;
//loop through the years, current to future.
for ($i=$start_year; $i<=$end_year; $i++) {
//your logic here, for now we'll just echo it out
echo $i."<br/>";
}

That’s it. It took me about 5 minutes to write this whole post, including the code. I am pretty sure it takes longer for some developers to copy-paste just the years of birth and even longer to find the place and change it a year later.

Let me know if you need any other dynamic date or year examples. Just thought since it is the end of the year, you may have to change the copyright year in a footer. :)

test .htaccess

Filed under: Web Development — rasim

A number of times I had to check why .htaccess was not working on a localhost or a server. Different machines have different setting and whether it is a wamp server, windows or linux box, I always check .htaccess file itself first. The easiest way to check if .htaccess settings get picked up is to create one with minimal settings. I like to have it redirecting to some other website as a confirmation. Below is the example of the content, just copy and place it in your .htaccess to see if it works:

Options +FollowSymLinks
rewriteEngine on
rewriteRule ^test\.html$ http://www.hackcorp.com/index.php [R=301,L]

Now go to test.html on your server and if your .htaccess gets accessed by the apache, you should get redirected to this site – http://www.hackcorp.com/index.php. If not, start looking into your apache settings. Make sure that the basic settings are set correctly, such as mod_rewrite module is enabled in httpd.conf and AllowOverride All is set on the directory in the httpd.conf.

Wheather it is a simple error of misplacing .htaccess or wamp and windows not seing the file, the first step of checking if the settings get picked up is often overlooked.

P.S. Don’t forget that apache server needs to be restarted every time you make a change. :)

Please let me know if this post was helpful or if you would like to see more details on the topic.

Still need to put
something up
in here.

Sorry, blog is
sort of new. :)

(Probably means that
ad space is cheap)