Home > Backend Development > PHP Tutorial > Chapter 10 Date and Time_PHP Tutorial

Chapter 10 Date and Time_PHP Tutorial

WBOY
Release: 2016-07-13 10:31:56
Original
863 people have browsed it

Learning points:
1.PHP date and time library

Dates and times are very different from most other types of data you encounter when programming in PHP.
Because date and time do not have a clear structure, and the calculation and representation of dates are also troublesome. In PHP, the date and time function library is a core part of the PHP language.

The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also known as Unix Timestamp. Unix timestamp, also known as Unix time (Unix time), POSIX time (POSIX time), is a time representation method, defined as starting from 00:00 Greenwich Mean Time on January 1, 1970 00

The total number of seconds since minute 00 seconds. Unix timestamps are not only used in Unix systems and Unix-like systems, but are also widely used in many other operating systems. For example (1184557366 means 2007-07-16 03:42:46)



1. PHP date and time library

Verification date: The checkdate() function can verify the date very well. If the provided date is valid, it returns true,

otherwise it returns false.


Format time and date:
<?<span php
</span><span if</span> (<span checkdate</span>(2,29,2007<span )) {
</span><span     echo</span> '日期合法'<span ;
} </span><span else</span><span  {
</span><span     echo</span> '日期不合法'<span ;
}
</span>?>
Copy after login
date

() function returns the string form of time and date formatted according to predefined instructions.

For all format parameters, please refer to the manual.


View more time information:
<?<span php
</span><span echo</span> <span date</span>('Y-m-d H:i:sa'); <span //</span><span 直接输入日期和时间</span>
<span echo</span> <span date</span>('今天的日期和时间为:Y/m/d H:i:sa'); <span //</span><span 可以插入无关的字符串</span>
?>
Copy after login
gettimeofday

() function returns an associative number

group composed of elements related to the current time.


Convert a timestamp into a friendly value: The
<?<span php
</span><span print_r</span>(<span gettimeofday</span>()); <span //</span><span 可以传入一个真(1)</span>
?>
Copy after login
getdate

() function accepts a timestamp and returns an associative array consisting of its parts

. If no parameters are given, the current time and date are returned.


Get the current timestamp: The
<?<span php
</span><span print_r</span>(<span getdate</span>(1184557366<span ));
</span>?>
Copy after login
time

() function can get the current timestamp and set the timestamp value.

Get a specific timestamp:
<?<span php
</span><span echo</span> <span date</span>('Y-m-d H:i:s',<span time</span>()+(7*24*60*60<span ));
</span>?>
Copy after login
mktime

() function can generate a timestamp for a given date and time.

Calculate time difference
<?<span php
</span><span echo</span> <span mktime</span>(14,14,14,11,11,2007<span );
</span><span echo</span> <span date</span>('Y-m-d H:i:s',<span mktime</span>(14,14,14,11,11,2007<span ));
</span>?>
Copy after login

Convert date to timestamp:
<?<span php
</span><span $now</span> = <span time</span><span ();
</span><span $taxday</span> = <span mktime</span>(0,0,0,7,17,2010<span );
</span><span echo</span> <span round</span>((<span $taxday</span> - <span $now</span>)/60/60<span );
</span>?>
Copy after login
strtotime

() converts human-readable date to Unix timestamp.

Calculate time difference
<?<span php
</span><span echo</span> <span strtotime</span>('2007-10-31 14:31:33'<span );
</span>?>
Copy after login

Get the last modification time of the current file:
<?<span php
</span><span echo</span> (<span strtotime</span>('2007-10-31 14:31:33') - <span strtotime</span>('2007-10-31 11:31:33'))/60/60<span ;
</span>?>
Copy after login
getlastmod

() can get the timestamp of the last modification time of the current file.

<?<span php
</span><span echo</span> <span date</span>('Y-m-d H:i:s',<span getlastmod</span><span ());
</span>?>
Copy after login
Set the time zone and GMT/UTC

:

Modify the settings in the php.ini file, find the;date.timezone = option under [date], and change this item to

date .timezone=Asia/Shanghai, and then restart the apache server. The

putenv

() function can set the current default time zone.


<?<span php
</span><span putenv</span>('TZ=Asia/Shanghai'<span );
</span><span echo</span> <span date</span>('Y-m-d H:i:s'<span );
</span>?>
Copy after login
date_default_timezone_set

() can set the current default time zone.

date_default_timezone_get() can get the current default time zone.

Get local time
<?<span php
date_default_timezone_set(</span>'Asia/Shanghai'<span );
</span><span echo</span> <span date</span>('Y-m-d H:i:s'<span );
</span>?>
Copy after login
localtime

() function can get local time data and then return an array.

Calculate the page script running time:
<?<span php
date_default_timezone_set(</span>'Asia/Shanghai'<span );
</span><span print_r</span>(<span localtime</span><span ());
</span><span print_r</span>(<span localtime</span>(<span time</span>(), <span true</span><span ));
</span>?>
Copy after login
microtime

() function, which returns the current UNIX timestamp and microseconds. Returns

a string in the format msec sec, where sec is the current UNIX timestamp and msec is the number of microseconds.


Note: The article comes from Li Yanhui’s PHP video tutorial. This article is for communication only and may not be used for commercial purposes, otherwise you will be responsible for the consequences.

<?<span php
</span><span function</span><span  fntime() {
    </span><span list</span>(<span $msec</span>, <span $sec</span>) = <span explode</span>(' ', <span microtime</span><span ());
    </span><span return</span> <span $msec</span>+<span $sec</span><span ;
}

</span><span $start_time</span> =<span  fntime();

</span><span for</span>(<span $i</span>=0;<span $i</span><1000000;<span $i</span>++<span ) {
    
}

</span><span $end_time</span> =<span  fntime();
</span><span echo</span> <span round</span>(<span $end_time</span> - <span $start_time</span>,4<span );
</span>?>
Copy after login

http://www.bkjia.com/PHPjc/759629.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/759629.htmlTechArticleLearning points: 1.PHP date and time library When programming in PHP, it is different from most other types you encounter The data is very different compared to date and time. Because the date and time are not clear...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template