php date 函数_PHP date()和time()函数教程

php date 函数

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 format

    d代表01和31格式的日期

  • D represents day in Mon to Sun format

    D代表星期一至星期日的日期

  • m represents the month in 01 to 12 format

    m表示01到12格式的月份

  • M represents month abbreviated Jan to Dec format

    M代表月份,缩写为Jan到Dec

  • y represents the year in two digits from 08 to 14 format

    y代表从08到14格式的两位数字的年份

  • Y represents the year in four digits 2008 or 2017 format

    Y以2008或2017的四位数表示年份

  • h represents hour in 12-hour format from01 to 12

    h表示从01到12的12小时格式的小时

  • H represents hour in 24-hour format from 00 to 24

    H表示小时的24小时格式,从00到24

  • i represents minutes in two digits 00 to 59

    i用两位数字表示分钟00到59

  • s represents seconds in two digits 00 to 59

    s以两位数字表示秒,00到59

  • a represents lowercase ante meridiem and post meridiem am or pm

    a代表小写的前子午线和后子午线上午或下午

  • 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
LEARN MORE  Python Timestamp Examples with Time Module
通过时间模块了解更多Python时间戳示例

翻译自: https://www.poftut.com/php-date-time/

php date 函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值