search
HomeBackend DevelopmentPHP Tutorialphp date function and php time function
php date function and php time functionJun 02, 2018 am 09:28 AM
phpfunctiontime

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:

<?php
var_dump(checkdate(12,31,2000));//var_dump函数显示表达式的类型与值
var_dump(checkdate(2,29,2003));
var_dump(checkdate(2,29,2004));
?>
显示:
bool(true)
bool(false)
bool(true)
例子2:
<?php
if (checkdate(12,31,2009))
{
echo "a";
}
else
{
echo "b";
}
?>

Display:

a

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

<?php
echo(date_default_timezone_set("Europe/Paris"));
?>

Display:

1


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

<?php
echo(date_default_timezone_get());
?>

Display:

Asia/Shanghai

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:

<?php
$t=time();
echo($t . "<br />");
echo(date("D F d Y",$t));
?>
1138618081//这是总秒数
Mon January 30 2006

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:

<?php
echo("date()的例子:<br />");
echo(date("l") . "<br />");//l - 星期几,英文全名; 如: "Friday" 
echo(date("l dS \of F Y h:i:s A") . "<br />");//l星期 d日期S英文序数\of显示of,F Y 月份年,A上下午
echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."<br />");
echo(date(DATE_RFC822) . "<br />");
echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br /><br />");
echo("gmdate()格林威治标准时的例子:<br />");
echo(gmdate("l") . "<br />");
echo(gmdate("l dS \of F Y h:i:s A") . "<br />");
echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."<br />");
echo(gmdate(DATE_RFC822) . "<br />");
echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br />");
?>

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

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

六:
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。
例子:

<?php
print_r(getdate());
?>

显示:

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

例子2:

<?php
$my_t=getdate(date("U"));
print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");
?>

显示:

Wednesday, January 25, 2006

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

<?php
echo(gettimeofday(true) . "<br /><br />");
print_r(gettimeofday());
?>

显示:

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

例子2:

<?php
$my_t=gettimeofday();
print("$my_t[sec].$my_t[usec]");
?>

显示:

1138197006.988273

八:
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() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:

<?php
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
?>

输出:

Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999

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

<?php
echo(strftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
//输出当前日期、时间和时区
echo(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time()));
?>

显示:

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

十:
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() 的值。
例子:

<?php
echo(idate("Y"));
?>

显示

2009

十一:
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(星期六)。
例子:

<?php
$localtime = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
print_r($localtime_assoc);
?>

显示:

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

十一:
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 是微秒部分。字符串的两部分都是以秒为单位返回的。
例子:

<?php
echo(microtime());
?>

显示:

0.25139300 1138197510

十二:
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 识别的部分
例子:

<php
$format="%d/%m/%Y %H:%M:%S";
$strf=strftime($format);
echo("$strf");
print_r(strptime($strf,$format));
?>

显示:

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] =>
)

十三:
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:

<?php
//计算葡萄牙里斯本的日出时间
//Latitude: 北纬 38.4 度
//Longitude: 西经 9 度
//Zenith ~= 90
//offset: +1 GMT
echo("Date: " . date("D M d Y") . "<br />");
echo("Sunrise time: ");
echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
?>

显示

Date: Tue Jan 24 2006
Sunrise time: 08:52

例子2 :

<?php
//计算葡萄牙里斯本的日落时间
//Latitude: 北纬 38.4 度
//Longitude: 西经 9 度
//Zenith ~= 90
//offset: +1 GMT
echo("Date: " . date("D M d Y") . "<br />");
echo("Sunrise time: ");
echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
?>

显示

Date: Tue Jan 24 2006
Sunrise time: 18:44

十四:
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() 函数页面中有说明。
例子:

<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

显示:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

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!

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怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

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 24, 2022 am 11:49 AM

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

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),