Php Date() & Time Function: How To Get Current Timestamp?

In this tutorial you will learn how lớn extract or format the date and time in PHPhường.

Bạn đ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 và 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 lớn read, PHPhường converts a timestamp lớn a format that is readable lớn 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 & time. However the timestamp is an optional parameter, if not included then current date & time will be used. The following statement displays today"s date:


Note: The PHP.. date() function return the current date và time according khổng lồ the built-in clock of the website VPS on which the script has been executed.


Formatting the Dates và Times with PHP

The format parameter of the date() function is in fact a string that can contain multiple characters allowing you khổng lồ generate a date string containing various components of the date & 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 lớn 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 lớn add additional visual formatting.

Xem thêm: Phần Mềm Radmin - Tư Vấn Mua Bản Quyền



Similarly you can use the following characters lớn format the time string:

h - Represent hour in 12-hour format with leading zeros (01 lớn 12) H - Represent hour in in 24-hour format with leading zeros (00 khổng lồ 23) i - Represent minutes with leading zeros (00 lớn 59) s - Represent seconds with leading zeros (00 lớn 59) a - Represent lowercase ante meridiem and post meridiem (am or pm) A - Represent uppercase Ante meridiem & Post meridiem (AM or PM)

The PHPhường. code in the following example displays the date in different formats:


Please check out the PHPhường date() function reference for a complete danh mục of all the characters that can be used inside the format parameter.

The PHPhường time() Function

The time() function is used khổng lồ 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.


1394003958

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


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


March 05, 2014 07:19:18

The PHP. mktime() Function

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