php date function and php time function

不言
Release: 2023-03-29 11:22:02
Original
2888 people have browsed it

This article mainly introduces the PHP date function and PHP time function. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

1:
checkdate()Function verifies a date.
checkdate(month,day,year)
If the specified value is legal, this function returns true, otherwise it returns false.
Date is legal in the following situations:
month is between and including 1 - 12
The value of Day is within the range of the number of days that a given month should have, leap years have been taken into account.
year is between and including 1 to 32767
Example 1:

 显示: bool(true) bool(false) bool(true) 例子2: 
Copy after login

Display:

a
Copy after login

date_default_timezone_set()Function settings are used in scripts The default time zone for all date/time functions in .
date_default_timezone_set(timezone)
timezone Required. Time zone identifier, such as "UTC" or "Europe/Paris".
List of legal time zones: http://www.php.net/manual/en/timezones.php
Example

Copy after login

Display:

1
Copy after login


date_default_timezone_get()The function returns the default time zone used by all date and time functions in the script.
date_default_timezone_get(void)
void optional

Copy after login

Display:

Asia/Shanghai
Copy after login

Four:
time()The function returns the current time Unix timestamp.
Returns the number of seconds since the Unix epoch (January 1, 1970 00:00:00 GMT) to the current time.
Since PHP 5.1, the timestamp of the time when the request was initiated is saved in $_SERVER['REQUEST_TIME'].
Example:

"); echo(date("D F d Y",$t)); ?>
Copy after login
1138618081//这是总秒数 Mon January 30 2006
Copy after login

Five:
date()Function formats a local time/date.
gmdate() function formats GMT/UTC date/time. Similar to the date() function, except that the time returned is Greenwich Mean Time (GMT).
date(format,timestamp)
format must specify how to return the result
a - "am" or "pm"
A - "AM" or "PM"
d - several Day, two digits, if there are less than two digits, add zeros in front; For example: "01" to "31"
D - Day of the week, three English letters; For example: "Fri"
F - Month, in English Full name; e.g.: "January"
h - hour in 12-hour format; e.g.: "01" to "12"
H - hour in 24-hour format; e.g.: "00" to "23"
g - The hour in 12-hour format, do not add zeros if there are less than two digits; for example: "1" to 12"
G - The hour of 24-hour format, do not add zeros if there are less than two digits; such as: "0" to "23" "
i - minutes; such as: "00" to "59"
j - days, two digits, if there are less than two digits, no zeros are added; such as: "1" to "31"
l - Day of the week, full English name; For example: "Friday"
m - Month, two digits, if there are less than two digits, add zeros in front; For example: "01" to "12"
n - Month, Two digits, if there are less than two digits, no zeros will be added; For example: "1" to "12"
M - Month, three English letters; For example: "Jan"
s - Seconds; For example: "00 " to "59"
S - add an English ordinal number at the end of the word, two English letters; such as: "th", "nd"
t - specify the number of days in the month; such as: "28" to "31"
U - Total seconds
w - Numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)
Y - Year, four digits; such as: "1999"
y - year, two digits; such as: "99"
z - day of the year; such as: "0" to "365"
Other characters not listed above are listed directly The character.
Example:

"); echo(date("l") . "
");//l - 星期几,英文全名; 如: "Friday" echo(date("l dS \of F Y h:i:s A") . "
");//l星期 d日期S英文序数\of显示of,F Y 月份年,A上下午 echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."
"); echo(date(DATE_RFC822) . "
"); echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "

"); echo("gmdate()格林威治标准时的例子:
"); echo(gmdate("l") . "
"); echo(gmdate("l dS \of F Y h:i:s A") . "
"); echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."
"); echo(gmdate(DATE_RFC822) . "
"); echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "
"); ?>
Copy after login

Display:
Example of date():

Tuesday Tuesday 23rd of June 2009 01:26:19 PM Oct 3,1975 was on a Friday Tue, 23 Jun 09 13:26:19 +0800 1975-10-03T00:00:00+08:00
Copy after login

gmdate() Example of Greenwich Mean Time:

Tuesday Tuesday 23rd of June 2009 05:26:19 AM Oct 3,1975 was on a Thursday Tue, 23 Jun 09 05:26:19 +0000 1975-10-02T16:00:00+00:00
Copy after login

六:
getdate()函数取得日期/时间信息。
getdate(timestamp)
timestamp可选 规定UNIX时间格式中的时间,没有则为当前时间
他返回一个根据 timestamp 得出的包含有日期信息的结合数组。如果没有给出时间戳,则认为是当前本地时间。

数组中的单元如下:
"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 纪元开始至今的秒数,和 time() 的返回值以及用于 date() 的值类似。 系统相关,典型值为从 -2147483648 到 2147483647。
例子:

Copy after login

显示:

Array ( [seconds] => 45 [minutes] => 52 [hours] => 14 [mday] => 24 [wday] => 2 [mon] => 1 [year] => 2006 [yday] => 23 [weekday] => Tuesday [month] => January [0] => 1138110765 )
Copy after login

例子2:

Copy after login

显示:

Wednesday, January 25, 2006
Copy after login

七:
gettimeofday()函数返回一个包含当前时间信息的数组。
所返回的数组键的含义是:
"sec" - 自 Unix 纪元起的秒数
"usec" - 微秒数
"minuteswest" - 格林威治向西的分钟数
"dsttime" - 夏令时修正的类型
gettimeofday(return_float)
return_float 可选。当其设置为 TRUE 时,gettimeofday() 会返回一个浮点数。
例子1


"); print_r(gettimeofday()); ?>
Copy after login

显示:

1138111447.4
Copy after login
Array ( [sec] => 1138111447 [usec] => 395863 [minuteswest] => -60 [dsttime] => 0 )
Copy after login

例子2:

Copy after login

显示:

1138197006.988273
Copy after login

八:
mktime()函数返回一个日期的 Unix 时间戳。
gmmktime()函数取得 GMT 日期的 UNIX 时间戳。与 mktime() 类似,不同的是返回值是格林威治标准时的时间戳。
参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。与 mktime() 一样,参数可以从右到左依次空着,空着的参数会被设为

相应的当前 GMT 值。
mktime(hour,minute,second,month,day,year,is_dst)
参数可以从右到左依次空着,空着的参数会被设为相应的当前 格林威治GMT 值。
hour 可选。规定小时。
minute 可选。规定分钟。
second 可选。规定秒。
month 可选。规定用数字表示的月。
day 可选。规定天。
year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
is_dst 可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。
自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。
例子
mktime() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:

Copy after login

输出:

Jan-05-2002 Feb-01-2002 Jan-01-2001 Jan-01-1999
Copy after login

九:
strftime()函数根据区域设置格式化本地时间/日期。
gmstrftime()函数根据本地区域设置格式化 GMT/UTC 时间/日期。与 strftime() 的行为相同,不同的是返回时间是格林威治标准时(GMT
)。
strftime(format,timestamp)
format 可选。规定如何返回结果。
timestamp 可选。
例子:

Copy after login

显示:

Dec 31 1998 20:00:00 Dec 31 1998 19:00:00 It is Wed on Jan 25, 2006, 11:32:10 time zone: W. Europe Standard Time
Copy after login

十:
idate()函数将本地时间/日期格式化为整数。
与 date() 不同,idate() 只接受一个字符作为 format 参数。
strftime(format,timestamp)
format 可选。规定如何返回结果。
B Swatch Beat/Internet Time
d 月份中的第几天
h 小时(12 小时格式)
H 小时(24 小时格式)
i 分钟
I 如果启用夏时制则返回 1,否则返回 0
L 如果是闰年则返回 1,否则返回 0
m 月份的数字
s 秒数
t 本月的总天数
U 自 Unix 纪元(January 1 1970 00:00:00 GMT)起的秒数――这和 time() 作用相同
w 星期中的第几天(星期天是 0)
W ISO-8601 格式年份中的第几个星期,每星期从星期一开始
y 年份(1 或 2 位数字――见下面说明)
Y 年份(4 位数字)
z 年份中的第几天
Z 以秒为单位的时区偏移量
timestamp 可选。默认值为本地当前时间,即 time() 的值。
例子:

Copy after login

显示

2009
Copy after login

十一:
localtime()函数返回本地时间(一个数组)。
localtime(timestamp,is_associative)参数 描述
timestamp 可选。规定被格式化的日期或时间。若未规定 timestamp,则使用当前的本地时间。
is_associative 可选。规定返回索引数组还是关联数组。
localtime() 的第一个参数是时间戳,如果没有给出则使用从 time() 返回的当前时间。
第二个参数是 is_associative,如果设为 false 或未提供则返回的是普通的数字索引数组。如果该参数设为 true 则 localtime() 函数返
回一个关联数组。
关联数组中不同的键名是:
"tm_sec" - 秒数
"tm_min" - 分钟数
"tm_hour" - 小时
"tm_mday" - 月份中的第几日
"tm_mon" - 年份中的第几个月,从 0 开始表示一月
"tm_year" - 年份,从 1900 开始
"tm_wday" - 星期中的第几天
"tm_yday" - 一年中的第几天
"tm_isdst" - 夏令时当前是否生效
注释:月份从 0(一月)到 11(十二月),星期数从 0(星期天)到 6(星期六)。
例子:

Copy after login

显示:

Array ( [0] => 24 [1] => 3 [2] => 19 [3] => 3 [4] => 3 [5] => 105 [6] => 0 [7] => 92 [9] => 1 )
Copy after login

十一:
microtime()函数返回当前 Unix 时间戳和微秒数。
microtime(get_as_float)
get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到
现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
例子:

Copy after login

显示:

0.25139300 1138197510
Copy after login

十二:
strptime()函数解析由 strftime() 生成的日期/时间。
strptime(date,format)
date 要解析的字符串(例如从 strftime() 返回的)。
format date 所使用的格式(与 strftime() 中所使用的相同)。
strptime() 返回一个将 date 解析后的数组,如果出错返回 FALSE。
月份和星期几的名字以及其它与语种有关的字符串对应于 setlocale()设定的当前区域(LC_TIME)。
数组中包含以下单元:
键名 说明
tm_sec 当前分钟内的秒数(0-61)
tm_min 当前小时内的分钟数(0-59)
tm_hour 午夜起的小时数(0-23)
tm_mday 月份中的第几天(1-31)
tm_mon 自一月起过了几个月(0-11)
tm_year 自 1900 年起过了几年
tm_wday 自星期天起过了几天(0-6)
tm_yday 本年自一月一日起过了多少天(0-365)
unparsed date 中未能通过指定的 format 识别的部分
例子:

Copy after login

显示:

03/10/2005 13:23:44 Array ( [tm_sec] => 44 [tm_min] => 23 [tm_hour] => 13 [tm_mday] => 3 [tm_mon] => 9 [tm_year] => 105 [tm_wday] => 0 [tm_yday] => 276 [unparsed] => )
Copy after login

十三:
date_sunrise()函数返回指定的日期与地点的日出时间。
date_sunrise(timestamp,format,latitude,longitude,zenith,gmt_offset)
date_sunset()函数返回指定的日期与地点的日落时间。
date_sunset(timestamp,format,latitude,longitude,zenith,gmt_offset)
1timestamp 必需。
2format 可选。规定如何返回结果:
SUNFUNCS_RET_STRING (以 string 格式返回结果,比如 16:46)
SUNFUNCS_RET_DOUBLE (以 float 格式返回结果,比如 16.78243132)
SUNFUNCS_RET_TIMESTAMP (以 integer 格式(时间戳)返回结果,比如 1095034606)
3latitude 可选。规定地点的纬度。默认是指北纬。因此如果要指定南纬,必须传递一个负值。
4longitude 可选。规定地点的经度。默认是指东经。因此如果要指定西经,必须传递一个负值。
5zenith 可选。
6gmt_offset 可选。规定 GMT 与本地时间的差值。单位是小时。
例子1:

"); echo("Sunrise time: "); echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); ?>
Copy after login

显示

Date: Tue Jan 24 2006 Sunrise time: 08:52
Copy after login

例子2 :

"); echo("Sunrise time: "); echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); ?>
Copy after login

显示

Date: Tue Jan 24 2006 Sunrise time: 18:44
Copy after login

十四:
strtotime()函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
strtotime(time,now)
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
该函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值
相对于 now 参数给出的时间,如果没有提供此参数,则用系统当前时间。
该函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程
在 date_default_timezone_get() 函数页面中有说明。
例子:

Copy after login

显示:

1138614504 1128290400 1138632504 1139219304 1139503709 1139180400 1138489200
Copy after login

The above is the detailed content of php date function and php time function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!