search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP strtotime function_PHP tutorial
Detailed explanation of PHP strtotime function_PHP tutorialJul 21, 2016 pm 03:41 PM
phpstrtotimeintroducefunctionWillmanualtextdatetimeofEnglishDetailed explanation

First read the manual introduction:

strtotime — Parse the date and time description of any English text into a Unix timestamp
format: int strtotime ( string $time [, int $now ] )
This function is expected Accepts a string containing a date in US English format and attempts to parse it into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT) relative to the time given by the now argument, if not provided This parameter uses the current system time.
This function will use the TZ environment variable (if any) to calculate the timestamp. Since PHP 5.1.0 there is an easier way to define the time zone used in all date/time functions. This process is documented on the date_default_timezone_get() function page.
Note: If the given year is in two-digit format, the values ​​0-69 represent 2000-2069 and 70-100 represent 1970-2000.

Parameters
time
The string to be parsed, formatted according to the syntax of GNU » Date Input Format. Before PHP 5.0, milliseconds were not allowed in time. Since PHP 5.0, they are allowed but will be ignored.
now
The timestamp used to calculate the return value. The default value of this parameter is the current time time(), and can also be set to a timestamp of other times (a feature I have always ignored, I am ashamed)
Return value: Returns the timestamp if successful, otherwise returns FALSE. Before PHP 5.1.0, this function returned -1 on failure, and later versions returned false.

The first parameter of strtotime can be our common English time format, such as "2008-8-20" or "10 September 2000" and so on. It can also be a time description based on the parameter now, such as "+1 day" and so on.


The following is the list of available parameters for the latter method, where "current time" refers to the value of the second parameter now of strtotime, and the default is the current time
1. Month, Japanese and English List of names and their common abbreviations:
january,february,march,april,may,june,july,august,september,sept,october,november,december,
sunday,monday,tuesday,tues,wednesday,wednes , thursday,thur,thurs,friday,saturday

2. Time parameters and detailed description:
am : the time is before noon AM
pm: the time is noon or later afternoon
year: one year; for example, "next year" >fortnight: two weeks; for example, “a fortnight ago” Two weeks, for example, “a fortnight ago” means two weeks ago
week: one week Week
day: a day Day
hour: an hour Hour
minute: a minute Minute
min: same as minute Same as “minute”
second: a second Second
sec: same as second Same as “second”

3. Related And sequence description:
+n/-n: Calculate the current time, plus or minus the specified time, for example, "+1 hour" means the current time plus one hour
ago: time relative to now; such as "24 hours ago" Calculates forward based on the current time, for example, "24 hours ago" represents "24 hours ago"
tomorrow: 24 hours later than the current date and time Based on the current time (including date and time), The same time tomorrow
yesterday : 24 hours earlier than the current date and time Based on the current time (including date and time), the same time yesterday
today : the current date and time Current time (including date and time)
now : the current date and time Current time (including date and time)
last : modifier meaning “the preceding”; for example, “last tuesday” represents “previous”, such as “last tuesday” represents “ same time last Tuesday”
this : the given time during the current day or the next occurrence of the given time; for example, “this 7am” gives the timestamp for 07:00 on the current day, while “this week ” gives the timestamp for one week from the current time. The specified time of the day or the timestamp of the following time period. For example, “this 7am” gives the timestamp of 7:00 that day, and “this week” gives the timestamp from the current time. The timestamp of the whole week from the beginning of time, which is the current time (tested by me: strtotime('this week')=strtotime('now'));
next: modifier meaning the current time value of the subject plus one; for example, “next hour” The current time plus the specified time, for example, “next hour” refers to the current time plus one hour, that is, plus 3600

//Come here first, the following is not yet Time translation
first : ordinal modifier, esp. for months; for example, “May first” (actually, it's just the same as next)
third : see first (note that there is no “second” for ordinality , since that would conflict with the second time value)
fourth : see first
fifth : see first
sixth : see first
seventh : see first
eighth : see first
ninth : see first
tenth : see first
eleventh : see first
twelfth : see first

4. Time zone description:
gmt : Greenwich Mean Time
ut : Coordinated Universal Time
utc : same as ut
wet : Western European Time
bst : British Summer Time
wat : West Africa Time
at : Azores Time
ast : Atlantic Standard Time
adt : Atlantic Daylight Time
est : Eastern Standard Time
edt : Eastern Daylight Time
cst : Central Standard Time
cdt : Central Daylight Time
mst : Mountain Standard Time
mdt : Mountain Daylight Time
pst : Pacific Standard Time
pdt : Pacific Daylight Time
yst : Yukon Standard Time
ydt : Yukon Daylight Time
hst : Hawaii Standard Time
hdt : Hawaii Daylight Time
cat : Central Alaska Time
akst : Alaska Standard Time
akdt : Alaska Daylight Time
ahst : Alaska-Hawaii Standard Time
nt : Nome Time
idlw : International Date Line West
cet : Central European Time
met : Middle European Time
mewt : Middle European Winter Time
mest : Middle European Summer Time
mesz : Middle European Summer Time
swt : Swedish Winter Time
sst : Swedish Summer Time
fwt : French Winter Time
fst : French Summer Time
eet : Eastern Europe Time, USSR Zone 1
bt : Baghdad Time, USSR Zone 2
zp4 : USSR Zone 3
zp5 : USSR Zone 4
zp6 : USSR Zone 5
wast : West Australian Standard Time
wadt : West Australian Daylight Time
cct : China Coast Time , USSR Zone 7
jst : Japan Standard Time, USSR Zone 8
east : Eastern Australian Standard Time
eadt : Eastern Australian Daylight Time
gst : Guam Standard Time, USSR Zone 9
nzt : New Zealand Time
nzst : New Zealand Standard Time
nzdt : New Zealand Daylight Time
idle : International Date Line East

There is a function called strtotime in PHP.strtotime implementation function: get the timestamp of a certain date, or get the timestamp of a certain time. strtotime parses the date and time description of any English text into a Unix timestamp [convert system time into a unix timestamp]

1. Get the unix timestamp of the specified date

strtotime("2009-1-22") Example is as follows:
1.echo strtotime("2009-1-22")
Result: 1232553600
Description: Return to 0:00 on January 22, 2009 0 minutes and 0 seconds timestamp

2. Get the English text date and time

The example is as follows:
For easy comparison, use date to compare the current timestamp with the specified timestamp Convert to system time
(1) Print the timestamp of tomorrow at this time strtotime("+1 day")
Current time:
1.echo date("Y-m-d H:i:s",time( ))
Result: 2009-01-22 09:40:25
Specified time:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
Result: 2009-01-23 09:40:25
(2) Print the timestamp of yesterday at this time strtotime("-1 day")
Current time:
1.echo date(" Y-m-d H:i:s",time())
Result: 2009-01-22 09:40:25
Specified time:
1.echo date("Y-m-d H:i:s", strtotime("-1 day"))
Result: 2009-01-21 09:40:25
(3) Print the timestamp at this time next week strtotime("+1 week")
Current time:
1.echo date("Y-m-d H:i:s",time())
Result: 2009-01-22 09:40:25
Specified time:
1. echo date("Y-m-d H:i:s",strtotime("+1 week"))
Result: 2009-01-29 09:40:25
(4) Print the time at this time last week Poke strtotime("-1 week")
Current time:
1.echo date("Y-m-d H:i:s",time())
Result: 2009-01-22 09:40: 25
Specified time:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
Result: 2009-01-15 09:40:25
(5) Print the timestamp of the specified next day of the week strtotime("next Thursday")
Current time:
1.echo date("Y-m-d H:i:s",time())
Result :2009-01-22 09:40:25
Specified time:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
Result: 2009-01 -29 00:00:00
(6) Print the timestamp of the specified day of the week strtotime("last Thursday")
Current time:
1.echo date("Y-m-d H:i:s" ,time())
Result: 2009-01-22 09:40:25
Specified time:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday") )
Result: 2009-01-15 00:00:00
As can be seen from the above example, strtotime can parse the date and time description of any English text into a Unix timestamp. We format it in conjunction with mktime() or date() Date and time gets the specified timestamp to achieve the required date and time.
I hope that after the introduction of this article, you can master the usage of strtotime function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321108.htmlTechArticleFirst read the manual introduction: strtotime - parse the date and time description of any English text into Unix timestamp format: int strtotime ( string $time [, int $now ] ) This function is expected to accept a...
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字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

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 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

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(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.