Advertisement

Display current time using PHP

Display time using PHP


Today, I will tell you how to display current time in this format : HH:MM:SS ( Hours:Minutes:Seconds )
We will use PHP’s Built in Function : time(); and another one : date(); in a combination to set our time according to our Time Zone.
The most easiest example is :

PHP Code :
date("h:i:s");
Which prints(for me) : 12:08:28
This is the thing we want, however, we we will make some edits in this code to set it according to our timezone.

PHP Code:
<?php
$time_offset ="0"; // Change this to your time zone
$time_a = ($time_offset * 120);
$time = date("h:i:s",time() + $time_a);
echo 'Current time is : '.$time;
?>

This code, for me, prints : Current time is : 12:13:31
So guys, You can use this little snippet of PHP Code to display current time, or, you can try some of these combinations and modify the above scirpt to get the results you want:

PHP Code:
<?php
// Assuming today is: June 13th, 2008, 12:14:18 pm

$time = date(”F j, Y, g:i a”); // June 13, 2008, 12:16 pm
$time = date(”m.d.y”); // 06.13.08
$time = date(”j, n, Y”); // 13, 6, 2008
$time = date(”Ymd”); // 20080613
$time = date(’h-i-s, j-m-y, it is w Day z ‘); // 12-16-41, 13-06-08, 1630 1641 5 Fripm08 164
$time = date(’\i\t \i\s \t\h\e jS \d\a\y.’); // It is the 13th day.
$time = date(”D M j G:i:s T Y”); // Fri Jun 13 12:17:06 PKT 2008
$time = date(’H:m:s \m \i\s\ \m\o\n\t\h’); // 12:06:18 m is month
$time = date(”H:i:s”); // 12:17:32
?>

So, keep practicing with different combination :)

Related Tutorials



3 responses. Wanna say something?

    Comment #1
  1. Venura
    Posted on Jul 22, 2008 at 18:15:55

    Great Job !!!!!

    pretty much useful………

  2. Comment #2
  3. Sridhar
    Posted on Aug 2, 2008 at 10:33:02

    how to use time function. I want to get local system time when submit button is clicked.

  4. Comment #3
  5. Bilal Ghouri
    Posted on Aug 2, 2008 at 16:25:46

    @Sridhar : You can use it like this :

    < ?php
    if (isset($_POST['submit-button-name']))
    {
    // do the functions to display time..
    }
    ?>
    Hope it helps.

Post a Comment