Time Saving PHP Code Snippets

These are super time-savers! It takes a little while to add them to existing sites, but once that's done it saves a ton of time in the long run. And to save even more time they can be put into PHP includes.

Note: If it makes more sense to you, you can use print instead of echo, which does the same thing.

Automatically Updating Copyright Date

With this code, you put in the year the site first went online, and then the current year will automatically appear, getting the current year from the server. That means there's no need to update the copyright each new year; it automatically changes on January 1st without needing to touch it.

<p> Copyright &copy;
$startYear = 2000;
$thisYear = date('Y');
if ($startYear == $thisYear)
{echo $startYear; }
{echo "{$startYear}-{$thisYear}"; }
Your Site Name. All rights reserved.</p>

If this is the first year your site is online, only the current year will appear. If the site went up in a previous year, both that year and the current year will show.

Last Modified Date

Instead of having to remember to change the "last updated" date on pages when they're updated, for those who use this feature, this will automatically put in the current date when the page is uploaded. PHP will get it from the server time.

<p><?php echo "Last modified: ",date ("l, F d, Y", getlastmod());?></p>