Last Updated on: September 16, 2022
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.
Get notified of new posts: