Quick Reach
The Time function of PHP
The time PHP function returns the total umber of seconds since the Unix Epoch (January 1, 1970, 00:00:00 GMT).
Experience this example online
1
2
3
4
5
|
<?php
echo time();
?>
|
As you can see, the output is a long number that generally is not understandable by the end user in terms of presenting the time to your web pages.You have to calculate this in order to understand the output.
To make it meaningful, you have to use it with date() function in order to format it and making it understandable as shown below.
The time function example with date
The following example will display the date on the basis of returned value of PHP time function.
Experience this example online
1
2
3
4
5
6
7
8
9
|
<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>
|
In the above example, we used the time function and assigned to $t variable that is used as a parameter of the date function. Then it displayed the Year, month and day on that basis.
Time in hour: minute: second format example
Following example shows using the time function with the date to display the time.
Experience this example online
1
2
3
4
|
<?php
$t=time();
echo(date("h:i:s",$t));
?>
|
The output will be according to your system time where the script is running.
Time and date display example
The following example displays the date and time by using the time and date function in m/d/y h: i: s format.
Experience this example online
1
2
3
4
|
<?php
$t=time();
echo(date("m/d/y h:i:s",$t));
?>
|
The output will be current date and time in the given format.
For more on time formatting also see date() function and PHP time parameters.
Also read: PHP mktime | timezone usage | getdate() function
Leave A Comment?