Popular Post: Free PHP Date Display Script

Free PHP Date Display Script

21 April 2007 (22:36) | Free PHP Scripts

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.

Your Ad Here

Downloads and Demos

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):

  1. put help comments into separate readme file
  2. create different date and time display examples
  3. separate hours variable into required file for global updating
  4. have the option to update the hours variable online in simple form
  5. create if|elseif|else statement to automatically allow for daylight savings time without manual intervention
  6. create 3-line version of script (done)
  7. create a version with multiple timezone display
  8. update to full open source licence, so you can do whatever you like with this script (no credit required) (done)


You can find our script on Hotscripts (under PHPDate and Time) along with more than 12,000 other PHP scripts!


For PHP tutorials, start at www.php.net’s links page. If you want to change the date format, see the options at: www.php.net/manual/function.date.php

Actual Code

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:


// melbdate.php3
// 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.
// http://www.gnu.org/copyleft/gpl.html

// 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);
print ("$melbdate");


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));
print ("$melbdate");


Link To This Page
If you found this page useful, consider linking to it.
Simply copy and paste the code below into your web site (Ctrl+C to copy)
It will look like this: Free PHP Date Display Script


Comment from sopitikoj
Date: 8 September 2007, 12:55 pm

Hi all!

So interesting site, thanks!


Comment from PHP Encoder
Date: 21 February 2008, 10:53 pm

Nice article and thanks for your information,
very useful resource!

Comment from jp50
Date: 2 March 2008, 4:15 pm

Thanks for your datetime functions.
Great help .

Comment from Torpedic
Date: 13 April 2008, 7:06 am

i merge this script with javasript clock at page http://www.current-world-time.com :)

Comment from Xyso
Date: 23 July 2008, 4:31 am

Thanks so much for the time and date script!

Comment from ozman
Date: 24 July 2008, 2:58 am

how to use it inside html page ?
– do i need to put php script outside tag.

Comment from Dean
Date: 15 December 2008, 10:00 pm

@Torpedic: great idea … easy to make a live “local” clock that way!

@ozman: Yes, you need the ” < ?php " (without a space before the question mark) and " ? > ” at the end — I’ve included it in the code above. If your page has a .html extension, you’d need to set the server web settings so that the server treats your “.html” extension as a “.php” file type.

Thanks for the other comments!

Comment from Tashi Wangchuk
Date: 15 February 2009, 10:51 pm

My date() function could display only current year’s date. How can i make it to display date of other years??Thank you…Hope to hear from some one atleast.

Comment from nelson
Date: 24 May 2009, 12:30 am

the date script is very good,please keep up the good work

Comment from marked
Date: 4 June 2009, 12:54 pm

OH yeah! it works man! a lot of thanks.

Comment from Website Design Kent
Date: 31 August 2009, 4:42 pm

Great script for people with no php knowledge.
Create a .htacess file and add the following “AddType x-mapp-php4 .html .htm” – without quotes, to enable php parsing on html pages. Upload the .htaccess file to your root of your domain.

Comment from Anton
Date: 2 September 2009, 9:57 pm

Thank You !!! AWESOME!!! Just the 3 lines !!! was HELPFUL!!!!


Comment from Karthick
Date: 9 October 2009, 5:30 pm

Hello There,

I am very Pleased to introduce our new website ScriptsBase.com. Scripts Base is intended to be the largest source uncomparable with others for absolute free or commercial website development scripts in ASP, .NET, PHP, JAVA, CGI, PERL and much more. Webmasters can easily find accurate, amazing, interesting, capturing, exiting, tarnishing, resolutions for their website.

Please feel free to submit your scripts to our website here (http://www.scriptsbase.com/submit-script/) for inclusion in our directory.

Pleasure will be continued in rendering our service with our assurance that website will stand as outstanding performance comparatively and help you better and best among all marketed and makes you to feel envy by others and will have definitely pride achievement to the maximum and success for your assignment.

Thanking You,

Site Admin,

Comment from Afjal
Date: 24 October 2009, 5:16 pm

Hi This is good discussion,

I have a question on displaying time on the website !

here it is:

I want to display Local time on my website, means if i open the website in USA(California) then it should show the PST zone, if i open the website in Dubai then it should show Dubai time, if i open the website in India then it should show the Indian Standard time (IST)

any idea how to do this?

Thanks Alot

Comment from Chad
Date: 23 December 2009, 12:19 pm

Can this script change the hour +1 if the minutes are above 56 or any set variable ? if so could you please inform me how to implement that change ?


Comment from Oeyvind
Date: 22 January 2010, 7:15 pm

Hi Dean
Do you have a script that make dynamic adjustment between browsertime and local time ?
I have a script that emails me an errormessage in case on database failure and want it to show my local time independent of where in the world the browsing (that set off the errormessage) was done.

Write a comment