php date 函数
Php provides a very enhanced date and time functions. There are a lot of different use cases for date and time functions. We will try to look at most of them in this tutorial.
PHP提供了非常增强的日期和时间功能。 日期和时间功能有很多不同的用例。 在本教程中,我们将尝试研究其中的大多数内容。
PHP date()函数 (PHP date() Function)
date()
function is used to get a date in different formats. date() function can be used to get timestamps too. Unix Timestamp is the second count from January 1, 1970. The syntax of the date function is like below. Here TIMESTAMP is optional so if we want to get the current date we do not need to provide TIMESTAMP.
date()
函数用于获取不同格式的日期。 date()函数也可用于获取时间戳。 Unix Timestamp是从1970年1月1日开始的第二个计数。date函数的语法如下。 此处的TIMESTAMP是可选的,因此如果我们想获取当前日期,则无需提供TIMESTAMP。
date(FORMAT,TIMESTAMP);
In this example, we will get the current date as day-month-year
format.
在此示例中,我们将以day-month-year
格式获取当前日期。
<?php
echo date('d-m-y');
?>
This will print the following date.
这将打印下一个日期。
05-05-2017
PHP date()函数格式 (PHP date() Function Formatting)
Php provides a rich set of printing options. In this part, we will look at formatting options. Here the list.
PHP提供了丰富的打印选项。 在这一部分中,我们将研究格式选项。 这里的清单。
d
represents day in 01 and 31 formatd
代表01和31格式的日期D
represents day in Mon to Sun formatD
代表星期一至星期日的日期m
represents the month in 01 to 12 formatm
表示01到12格式的月份M
represents month abbreviated Jan to Dec formatM
代表月份,缩写为Jan到Decy
represents the year in two digits from 08 to 14 formaty
代表从08到14格式的两位数字的年份Y
represents the year in four digits 2008 or 2017 formatY
以2008或2017的四位数表示年份h
represents hour in 12-hour format from01 to 12h
表示从01到12的12小时格式的小时H
represents hour in 24-hour format from 00 to 24H
表示小时的24小时格式,从00到24i
represents minutes in two digits 00 to 59i
用两位数字表示分钟00到59s
represents seconds in two digits 00 to 59s
以两位数字表示秒,00到59a
represents lowercase ante meridiem and post meridiem am or pma
代表小写的前子午线和后子午线上午或下午A represents lowercase ante meridiem and post meridiem AM or PM
A代表小写的前子午线和后子午线AM或PM
PHP time()函数 (PHP time() Function)
Another useful function provided by Php is time()
. The time() function is used to get the current time Unix timestamp. The current timestamp can get like the below example.
Php提供的另一个有用函数是time()
。 time()函数用于获取Unix时间戳的当前时间。 当前时间戳可以像下面的示例所示。
<?php
echo time();
?>
This will print an integer value like below.
这将打印一个整数值,如下所示。
1496680560
将时间戳转换为日期 (Convert Timestamp To Date)
We can convert the given timestamp into a date format. We will remember that date() function will get time stamp as a second argument. In this example we will give timestamp we get previously and print it more human-readable format.
我们可以将给定的时间戳转换为日期格式。 我们将记住,date()函数将获得时间戳作为第二个参数。 在此示例中,我们将提供之前获得的时间戳,并将其打印为更易于理解的格式。
<?php
$timestamp =1496680560;
echo date('d-m-y h:i',$timestamp);
?>
We will get the following output.
我们将获得以下输出。
05-06-17 07:36
PHP mktime()函数 (PHP mktime() Function)
Sometimes we need to generate timestamp with a given date and time. This means we do not want to get current time timestamp. In this situation, we can use mktime()
function. Syntax of mktime() function is like below.
有时我们需要生成具有给定日期和时间的时间戳。 这意味着我们不想获取当前时间的时间戳。 在这种情况下,我们可以使用mktime()
函数。 mktime()函数的语法如下。
mktime( hour , minute , second , month , day , year );
In this example, we will provide the first day of 2017.
在此示例中,我们将提供2017年的第一天。
<?php
echo mktime( 0 , 0 , 0 , 1 , 4 , 2017 );
?>
This code will generate the following output.
此代码将生成以下输出。
1483477200
php date 函数