In this tutorial you will learn how to extract or format the date and time in PHP.

Đang xem: Php date() & time function: how to get current timestamp?

The PHP Date() Function

The PHP date() function convert a timestamp to a more readable date and time.

The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ).

Since this is an impractical format for humans to read, PHP converts a timestamp to a format that is readable to humans and dates from your notation into a timestamp the computer understands. The syntax of the PHP date() function can be given with.

The format parameter in the date() function is required which specifies the format of returned date and time. However the timestamp is an optional parameter, if not included then current date and time will be used. The following statement displays today”s date:

Note: The PHP date() function return the current date and time according to the built-in clock of the web server on which the script has been executed.

Formatting the Dates and Times with PHP

The format parameter of the date() function is in fact a string that can contain multiple characters allowing you to generate a date string containing various components of the date and time, like day of the week, AM or PM, etc. Here are some the date-related formatting characters that are commonly used in format string:

d – Represent day of the month; two digits with leading zeros (01 or 31) D – Represent day of the week in text as an abbreviation (Mon to Sun) m – Represent month in numbers with leading zeros (01 or 12) M – Represent month in text, abbreviated (Jan to Dec) y – Represent year in two digits (08 or 14) Y – Represent year in four digits (2008 or 2014)

The parts of the date can be separated by inserting other characters, like hyphens (-), dots (.), slashes (/), or spaces to add additional visual formatting.

Xem thêm:

Similarly you can use the following characters to format the time string:

h – Represent hour in 12-hour format with leading zeros (01 to 12) H – Represent hour in in 24-hour format with leading zeros (00 to 23) i – Represent minutes with leading zeros (00 to 59) s – Represent seconds with leading zeros (00 to 59) a – Represent lowercase ante meridiem and post meridiem (am or pm) A – Represent uppercase Ante meridiem and Post meridiem (AM or PM)

The PHP code in the following example displays the date in different formats:

Please check out the PHP date() function reference for a complete list of all the characters that can be used inside the format parameter.

The PHP time() Function

The time() function is used to get the current time as a Unix timestamp (the number of seconds since the beginning of the Unix epoch: January 1 1970 00:00:00 GMT).

ExampleRun this code »
The above example produce the following output.


We can convert this timestamp to a human readable date through passing it to the previously introduce date() function.

ExampleRun this code »
The above example produce the following output.

Xem thêm: Cách Tính Md Là Đơn Vị Gì ? Công Thức Tính Cho Đơn Vị Md

March 05, 2014 07:19:18

The PHP mktime() Function

The mktime() function is used to create the timestamp based on a specific date and time. If no date and time is provided, the timestamp for the current date and time is returned.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *