Home  >  Article  >  Backend Development  >  Detailed explanation of the example of obtaining date information using the php getdate() function

Detailed explanation of the example of obtaining date information using the php getdate() function

怪我咯
怪我咯Original
2017-04-17 15:35:274479browse

PHP provides a variety of functions for obtaining time and date, in addition to obtaining the current UNIX timestamp through the time() function and obtaining the current time using the date function , and can also call the getdate() function to determine the current time. This chapter will introduce you to the "getdate()" function.

Usage of getdate() function:

getdate() function is to obtain the relevant information of the specified part of the date. Its syntax is as follows:

getdate(timestamp);
## The #getdate() function returns date and time information in the form of an array. If there is no parameter timestamp, the current time shall prevail. The associative array elements returned by this function are as follows:


Description of the associative array elements returned by the getdate() function

Key nameDescriptionReturn value example##"seconds" to ##"minutes"059Hour digits represent to The number of the day of the month represents 31The number of the day of the week represents 6The number of the month represents 12The complete year represented by 4 digits2003The numerical representation of the day of the yearFull text representation of the day of the weekJanuary>time() The return value of  is similar to the value used for date()-2147483648Example one
## The number of # seconds represents 059
Minute numbers represent to ##"hours"
023 ##"mday"
1 to "wday"
0 (meaning Sunday) to (meaning Saturday) "mon"
1 to "year"
For example: 1999 or ##"yday"
0 to 365 "weekday"
Sunday to Saturday ##"month"complete month Text representing
to December 0since The number of seconds since the beginning of the Unix epoch, and
. Depends on the system, typical values ​​are from to 2147483647. getdate() functionExample

This example uses getdate () function takes no parameters, the code is as follows:

<?php
$time=getdate();

print_r($time);

?>
The running result is as follows:

The getdate() function returns the date and sum in the form of an array Time information, so print_r() is used to print.

Example 2

Detailed explanation of the example of obtaining date information using the php getdate() functionUse the getdate() function to obtain the current date information of the system and output the return value of the function. The example code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$arr=getdate();

echo $arr[year]."-".$arr[mon]."-".$arr[mday]." ";

echo $arr[hours].":".$arr[minutes].":".$arr[seconds]." ".$arr[weekday]."<br/>";

echo "today is the $arr[yday]th of year";


?>

Run results As shown below: Detailed explanation of the example of obtaining date information using the php getdate() function

#The above is a simple application of our getdate() function.

We all know that there are 12 months in a year and 7 days in a week..., but the computer does not know that, so we have to check whether the date is valid. In the next section, we We will introduce the use of PHP functions to check the validity of dates.

The above is the detailed content of Detailed explanation of the example of obtaining date information using the php getdate() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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