search
HomeBackend DevelopmentPHP TutorialPHP date and time processing-Zheng Aqi (continued)_PHP tutorial

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$y_lnk2=$year>=2037?$year= 2037:$year+1; //Next year
//Month link
$m_lnk1=$month$ m_lnk2=$month>=12?$month=12:$month+1; //Next month
echo "";
//Output the year, click the "" link to jump to the next year
echo "";
//Output the month, click the " "Link to jump to next month
echo "";
echo " ";
for($i=0;$i{
echo "> ; "; //Output week array
}
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{
$date=$i+1 -$wd; //Calculate the position of the day in the table
if($i%7==0) echo ""; //The beginning of a line
echo " "; //End of line
}
echo "

".$year."year
>

" .$month."month
>
$wd_ar[$i]
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 "
";
?>

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...
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function