English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed explanation of the usage of UNIX timestamp in PHP

This article describes the usage of php UNIX timestamp. Shared for everyone's reference, as follows:

The timestamp is the creation, modification, and access time of the file attributes. Digital timestamp service is one of the Web website security services that can provide secure protection for the date and time information of electronic files.

The advantages of the timestamp are:

Available variable encryption values can prevent the illegal reuse of values after the value is stolen, playing the role of encryption. The timestamp mainly depends on time and generates a unique number within the agreed time period.

UNIX timestamp

In the UNIX system, the date and time are represented as the number of seconds since1970 years1months1The number of seconds from the start of the day to the current moment is called the UNIX timestamp, represented by32binary numbers. This time representation is supported in all operating systems, and the same time is represented by the same UNIX timestamp in UNIX and Windows, so there is no need to convert between different systems.

Currently, the UNIX timestamp is represented by32The binary number represents32The binary number range is (-2147483648~+2147483647),Due to the system's inability to support negative timestamps, the maximum time that can be represented by the UNIX timestamp is currently2038years1months19days3hours14minutes7seconds, the timestamp of this moment is2147483647After this time, it is necessary to expand the binary bits of the UNIX timestamp.

PHP gets the timestamp of a specified date

PHP applies the mktime() function to convert a time into a UNIX timestamp value.

Syntax as follows

mktime(hour,minute,second,month,day,year,is_dst)

Parameter Description
hour Optional. Specifies the hour.
minute Optional. Specifies the minute.
second Optional. Specifies the second.
month Optional. Specifies the month represented by a number.
day Optional. Specifies the day.
year Optional. Specifies the year. On some systems, the valid value ranges between 1901 - 2038 . However, in PHP 5 There is no such restriction in it.
is_dst

Optional. If the time is during Daylight Saving Time (DST), it is set to1If not set, it is set to 0, and if unknown, it is set to-1.

Since 5.1.0 Starting from, the is_dst parameter has been deprecated. Therefore, you should use the new timezone handling feature.


For example:

echo "Timestamp: ".mktime().'<br>';//Return the current timestamp
echo "Any date: ".date("Y",-m-d",mktime(0,0,0,2,21,1996
echo "Current date: ".date("Y",-m-d",mktime()).'<br>';

The running result is:

Timestamp:1458979695
Any date:1996-02-21
Current date: 2016-03-26

Get the current timestamp

PHP gets the current UNIX timestamp through the time() function.

Syntax as follows:

int time(void);

This function has no parameters and returns an integer value of the UNIX timestamp.

For example:

echo time()."<br>";//Output the current timestamp
$nextWeek = time()+(7*24*60*60);//One week consists of seven days, one day24hours, one hour60 minutes, one minute60 seconds
echo "Now: ".date("Y",-m-d")."<br>";
echo "Next Week: ".date("Y",-m-d",$nextWeek);

The running result is

1458980073
Now: 2016-03-26
Next Week: 2016-04-02

Parses English text date-time descriptions into UNIX timestamps

The strtotime() function parses any English text date-time description into a Unix timestamp.

Syntax

strtotime(time,now)

Parameter Description
time Specifies the time string to be parsed.
now Used to calculate the timestamp of the return value. If the parameter is omitted, the current time is used. 

For example:

echo(strtotime("now")).'<br>';
echo(strtotime("3 October 2005
echo(strtotime("+5 hours")).'<br>';
echo(strtotime("+1 week")).'<br>';
echo(strtotime("+1 week 3 days 7 hours 5 seconds")).'<br>';
echo(strtotime("next Monday")).'<br>';
echo(strtotime("last Sunday")).'<br>';

The running result is:

1458980310
1128268800
1458998310
1459585110
1459869515
1459094400
1458403200

If you want to view the time corresponding to this timestamp, for example:

echo date("Y-m-d H:i:s",strtotime("now")).'<br>';
echo date("Y-m-d H:i:s\

The running result is:

2016-03-26 16:21:32
2016-03-31 00:00:00

PS: This site also provides a Unix timestamp conversion tool that includes various common language methods for timestamp operations for your reference:

Unix Timestamp (timestamp) Conversion Tool:
http://tools.jb51.net/code/unixtime

Readers who are interested in more content related to PHP can check the special topics on this site: 'Summary of PHP Date and Time Usage', 'Complete Collection of PHP Array (Array) Operation Skills', 'PHP Data Structures and Algorithms Tutorial', 'Summary of PHP Program Design Algorithms', 'Summary of PHP Regular Expression Usage', 'Summary of PHP String (string) Usage', and 'Summary of Common Database Operation Skills in PHP'.

I hope the content described in this article will be helpful to everyone's PHP program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like