Free PHP Date Display Script
What’s this script for?
- Makes an adjustment between server time and local time
- Nice and simple to use!
- Only 4 lines required, commented lines can be removed
- Works in older versions of PHP (v3+)
This script is useful if you use a virtual hosting service for your web pages. In our case, the server we use (at Pair Networks) is in Pittsburgh, USA, while our business is located in Melbourne, Australia.
In cgi or php scripts we’ve used, to display the date correctly for local (Melbourne time), we need to make an adjustment, otherwise we’d be showing Pittsburgh, USA time (half a day or so behind Melbourne).
Here’s a small piece of PHP code you can use if you’re in a similar situation. See the comments in the script for all the information you’ll need.
Of course, you need PHP running on the server you use, and need to be able to transfer the script to your server. If you need help with any of that, contact your provider for support.
Downloads and Demos
- See the script in action (very basic demo): melbdate.php3
- You can also download a text version of the script. Just rename the file extension from .txt to .php3 or to .php when you use it on your web server.
And here’s the cut-down, 3-line version of the same script in action:
- Demo: melbdate_3lines.php.
- Download: Here is the text version to view or download. Again, just rename the file extension from .txt to .php3 or to .php when you use it on your web server.
Using the Script
If you’re using this on just one page or template section, just include the script within the php code and place the print line of code where you want the date to appear. Keep the four lines of code in their order, but there is no need to have them appear on four consecutive lines. By adding other $melbdate type variables, you could even have more than one date format display on the page.
If you’re using this script on a number of pages, consider having it as an ‘include’ so that any changes only have to be made to the script only once on your server. In reality, you could delete all of the comment lines (save for the copyright and version lines!) and you’d only have a short easy 4 line script!
Limitation: in this version, there is no automatic allowance for your local daylight saving time. Thus, the difference in hours needs to be manually updated when daylight saving time starts/ends (if it is observed where you are). That is why I suggest you make it an include, and only have one copy you need to update throughout your website. (I plan to make this a feature of the next version!).
Planned improvements (December 2007):
- put help comments into separate readme file
- create different date and time display examples
- separate hours variable into required file for global updating
- have the option to update the hours variable online in simple form
- create if|elseif|else statement to automatically allow for daylight savings time without manual intervention
create 3-line version of script(done)
- create a version with multiple timezone display
update to full open source licence, so you can do whatever you like with this script (no credit required)(done)
Here is the script. The time difference here is “14″, which is the difference between my local time and the server time during our (Australian) “standard” non-daylight savings time. When we change over to daylight savings time — late October — I manually adjust this time to be “16″. So here it is:
// rename file extension from .txt to .php3 or to .php
// Script copyright (C) 2000 Dean Kennedy, Terrabyte Communications
// Version 1.03 04 June 2006
// Free to use, modify and copy. You can safely delete all other
// commented lines and just leave the four line script
// Distributed under the terms of the GNU General Public License (GPL).
// There is NO WARRANTY, it is provided AS IS. Use it at your own risk.
// To adjust the format of the date, see www.php.net/manual/function.date.php
// If your local time is *behind* the server time, then change the "+" to
// a "-" in the $melbdate line
// If you don't know how many hours, then "uncomment" the three lines
// below by deleting "// " to see what the server time is (remember to
// put the comments "// " back when you've finished checking:
// $serverdate = date("l, d F Y h:i a");
// print ("$serverdate");
// print (" <p>");
// Variable for hours
$hourdiff = "14"; // hours difference between server time and local time
// Nothing needs to be changed below here unless you want to change
// the format of the date (see above for URL of options) or your local
// time is behind the server time
$timeadjust = ($hourdiff * 3600);
$melbdate = date("l, d F Y h:i a",time() + $timeadjust);
Here’s a mini 3-line version of the very same script:
$hourdiff = "14"; // hours diff btwn server and local time
$melbdate = date("l, d F Y h:i a",time() + ($hourdiff * 3600));