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

Jul 21, 2016 pm 03:26 PM
php and deal with data database date hour time type

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...
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

See all articles