Home > Backend Development > PHP Tutorial > PHP date and time processing-Zheng Aqi (continued)_PHP tutorial

PHP date and time processing-Zheng Aqi (continued)_PHP tutorial

WBOY
Release: 2016-07-21 15:26:42
Original
1159 people have browsed it

1. UNIX timestamp
When phpd processes data, especially when formatting time type data in the database, it is necessary to first convert the time type data into UNIX timestamp for processing. Different database systems are not compatible with the conversion of time type data
. In this case, the time needs to be converted into a UNIX timestamp. In this way, the cross-platform nature of different database systems is achieved.
2. Convert time to timestamp
If you want to convert the date and time expressed in strings into timestamp form, you can use the strtotime() function.
The syntax format is as follows:
int strtotime(string $time [, int $now])
For example:

Copy code The code is as follows :

echo strtotime('2009-03-05'); //Output 1236211200
echo strtotime('2009-03-05 10:24:30 '); //Output 1236248670
echo strtotime("10 September 2000"); //Output 968544000
?>

另一个取得日期的UNIX时间戳的函数是mktime()函数,
语法格式如下:
int mktime([int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year]]]]]])
3.获取日期和时间
1.data()函数
是将时间戳照给定的格式转化为具体的日期和时间字符串。
语法格式如下:
string date(string $format [, int $timestamp ])
说明:
$format指定了转化后的日期和时间的格式,
$timestamp是需要转化的时间戳,如果省略则使用本地当前时间,即默认值为time()函数的值。
time()函数返回当前时间的时间戳
date函数的$format参数的取值如下表。
表4.6 date()函数支持的格式代码

字 符

说 明

返回值例子

d

月份中的第几天,有前导零的2位数字

01~31

D

星期中的第几天,用3个字母表示

Mon到Sun

j

月份中的第几天,没有前导零

1~31

l

星期几,完整的文本格式

Sunday~Saturday

N

ISO-8601格式数字表示的星期中的第几天

1(星期一)~7(星期天)

S

每月天数后面的英文后缀,用2个字符表示

st、nd、rd或th,可以和j一起用

w

星期中的第几天,数字表示

0(星期天)~6(星期六)

z

年份中的第几天

0~366

W

ISO-8601格式年份中的第几周,每周从星期一开始

例如:42(当年的第42周)

F

月份,完整的文本格式,如January或March

January~December

m

数字表示的月份,有前导零

01~12

M

三个字母缩写表示的月份

Jan~Dec

n

数字表示的月份,没有前导零

1~12

t

给定月份所应有的天数

28~31

L

是否为闰年

如果是闰年为1,否则为0

o

ISO-8601格式年份数字。这和Y的值相同,只是如果ISO的星期数(W)属于前一年或下一年,则用那一年

例如:1999或2003

Y

4位数字完整表示的年份

例如:1999或2003

y

2位数字表示的年份

例如:99或03

a

小写的上午和下午值

am或pm

A

大写的上午和下午值

AM或PM

B

Swatch Internet标准时

000~999

g

小时,12小时格式,没有前导零

1~12

G

小时,24小时格式,没有前导零

0~23

h

小时,12小时格式,有前导零

01~12

H

小时,24小时格式,有前导零

00~23

i

有前导零的分钟数

00~59

s

秒数,有前导零

00~59

e

时区标志

例如:UTC,GMT,Atlantic/Azores

I

是否为夏令时

如果是夏令时为 1,否则为0

O

与格林尼治时间相差的小时数

例如:+0200

P

与格林尼治时间(GMT)的差别,小时和分钟之间用冒号分隔

例如:+02:00

T

本机所在的时区

例如:EST,MDT

Z

时区偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边的时区偏移量总是正的

-43200~43200

c

ISO 8601格式的日期

2004-02-12T15:19:21+00:00

r

RFC 822 格式的日期

Thu, 21 Dec 2000 16:01:07 +0200

U

从UNIX纪元开始至今的秒数

time()函数

2.getdate()函数
可以获得日期和时间信息数组,
语法格式如下:
array getdate([ int $timestamp ])
说明:$timestamp是要转化的时间戳,如果不给出则使用当前时间。
函数根据$timestamp返回一个包含日期和时间信息的数组,数组的键名和值如表4.7所示

键 名

说 明

值 的 例 子

seconds

秒的数字表示

0~59

minutes

分钟的数字表示

0~59

hours

小时的数字表示

0~23

mday

月份中第几天的数字表示

1~31

wday

星期中第几天的数字表示

0(表示星期天)~6(表示星期六)

mon

月份的数字表示

1~12

year

4位数字表示的完整年份

例如:1999或2003

yday

一年中第几天的数字表示

0~365

weekday

星期几的完整文本表示

Sunday~Saturday

month

月份的完整文本表示

January~December

0

自UNIX 纪元开始至今的秒数

系统相关,典型值从-2147483648~2147483647

4.6.4 Other date and time functions
1. Calculation of date and time
Copy code The code is as follows:

$oldtime=mktime(0,0,0,9,24,2008);
$newtime=mktime(0,0,0,10,12,2008);
$ days=($newtime-$oldtime)/(24*3600); //Calculate the number of days difference between two times
echo $days; //Output 18
?>

2. Check date
The checkdate() function can be used to check whether a date data is valid. The syntax format is as follows:
bool checkdate( int $month , int $day , int $year)
Copy code The code is as follows:

var_dump(checkdate(12,31,2000)); //Output bool( TRUE)
var_dump(checkdate(2,29,2001)); //Output bool(FALSE)
?>

3. Set the time zone
The system default is Greenwich Mean Time, so the current time displayed may differ from local time. PHP provides the function date_default_timezone_set() that can modify the time zone.
The syntax format is as follows:
bool date_default_timezone_set (string $timezone_identifier) ​​
The parameter $timezone_identifier is the time zone to be specified.
The available value in mainland China is Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order Chongqing, Shanghai, Urumqi). PRC can be used in Beijing time.
4.5 Example - Generate Calendar
Copy code The code is as follows:

$year= @$_GET['year']; //Get the year in the address bar
$month=@$_GET['month']; //Get the month in the address bar
if(empty($year))
$year=date("Y"); //Initialized to this year's year
if(empty($month))
$month=date("n"); //Initialized to this month's year Month
$day=date("j"); //Get the number of days of the day
$wd_ar=array("日","一","二","三","四","五","Saturday"); //Week array
$wd=date("w",mktime(0,0,0,$month,1,$year)); //Calculate whether the first day of the month is a week Year
//Year link
$y_lnk1=$year<=1970?$year=1970:$year-1; //Previous year
$y_lnk2=$year>=2037?$year= 2037:$year+1; //Next year
//Month link
$m_lnk1=$month<=1?$month=1:$month-1; //Last month
$ m_lnk2=$month>=12?$month=12:$month+1; //Next month
echo "";
//Output the year, click the "<" link to jump to the previous year, click the ">" link to jump to the next year
echo "";
//Output the month, click the "<" link to jump to the previous month, click "> "Link to jump to next month
echo "";
echo " ";
for($i=0;$i<7;$i++)
{
echo "";
$tnum=$wd+date("t",mktime(0,0,0,$month, 1,$year)); //Calculate the day of the week plus the number of days in the month
for($i=0;$i<$tnum;$i++)
{
$date=$i+1 -$wd; //Calculate the position of the day in the table
if($i%7==0) echo ""; //The beginning of a line
echo "if($i>=$wd)
{
if($date==$day&&$month==date("n")) //If it is the current day of the month, then The number of days is blackened
echo "".$day."";
else
echo $date; //Output the number of days
}
echo "< ;/td> ";
if($i%7==6) echo "
"; //End of line
}
echo "

<
".$year."year>

<
" .$month."month>
$wd_ar[$i]}
echo "
";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323881.htmlTechArticle1. UNIX timestamp phpd processes data, especially when formatting time type data in the database, you need to first Convert time type data into UNIX timestamp for processing. Different databases...
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