PHP date and time
Home - Tutorials - Date and time
In this article I will show you how to use the PHP date and time functions and how to format date and time output.
Step 1 - PHP date and time basics
PHP date and time
PHP date and time handling functions allows you to get the date and time from the server where the script is executed. Besides this you can manipulate, edit, format these values before displaying them. In this section I will show you the most important functions how to get date and time.
The PHP date() function
The most used function in date and time handling is the date() itself. This function returns with the actual date and time in the format you specified. However the function is able to convert a timestamp format into a human readable format as well. The complete syntax of the function is the following:
string date ( string $format [, int $timestamp] )
You can get the actual date and time information as follows:
Code:
Output:Display date: 2007-09-12 Display time: 14:34:21 Display date and time: 2007-09-12 14:34:21 Display date and time in long format: 2007 September 12, Wed - 2:34:21 PM
The complete list of available formatting parameters can be found in the date manual.
The mktime() function
Sometimes you want to get a timestamp in the past or in the future. You can do this using the mktime function. You need to specify the time and it returns with the Unix timestamp. The syntax is the following:
int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
Later you can use this timestamp in the above mentioned date function to display it in a format you want. So you can use it as like here:
Code:
Output:Date generated by mktime: 2006-05-15 Date generated by mktime with invalid day and month: 2007-04-04
So if you want to get the tomorrows date you can use the following code:
Code:
<?php echo "Tomorrow is: "; ?>
Output:Today is: 2007-09-12 Tomorrow is: 2007-09-13
Here we used mktime and added 1 day to the day parameter.
Tags: php date, php time, dat time, php datetime, date functions, time functions
| PHP date and time - Table of contents |
|---|
| Step 1 - PHP date and time basics |