DevChunks

Display the date and time with PHP

A simple but valuable feature of dynamic websites is displaying the current date and time.

With PHP, this is easy to do with the date() and time() functions and is a great way for beginners to get their feet wet:

echo date("l, F d, Y h:i" ,time()); // Displays the date as Day, Month Date, Year Time

Sometimes it is not sufficient to display the time in just your local time zone.

You might want to display the time from another country. It is common to display the current time in a few major cities simultaneously.

You could calculate the new times on the fly, but the code could soon get complicated, and it’s unnecessary.

This example uses the PEAR Date class to change the time zone from GTM to Indian Standard Time:

// Start by including the class
include ("Date.php");

// Initialize the date/time object
$d = new Date("2006-06-21 10:36:27");

// Set the time zone to GMT (Greenwich Mean Time)
$d->setTZByID("GMT");

// Convert it to IST (Indian Standard Time)
$d->convertTZByID("IST");

// Display the time in IST
echo $d->format("%A, %d %B %Y %T");

This can easily be modified to include different time zones according to your needs.

Share:

2 Responses

Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,

Iteration

If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the