简体   繁体   English

我不理解的PHP日期函数用法

[英]PHP date function usage that I don't understand

So I have been handed a WordPress site that was built by other devs and I'm in the process of figuring out how much of the customization works. 因此,我已经交给了由其他开发人员构建的WordPress网站,目前我正在研究自定义的工作量。 I've come across their function for archiving posts and I've encountered a use of the PHP date() function that I've never seen before (though admittedly, I haven't seen a whole lot) 我遇到了他们用于归档帖子的功能,并且遇到了从未使用过的PHP date()函数的使用(尽管可以肯定,我没有看到很多)

date('Y-1-1', strtotime('-1 year'))

Can someone explain that bit of it to me? 有人可以向我解释一下吗? I've never seen the parameter 'Y-1-1' before and I wasn't even aware you could pass other params to the date() function like that. 我之前从未见过参数“ Y-1-1”,甚至不知道您可以将其他参数传递给date()函数。 I played with the parameters a bit and I either see no change or I break it. 我玩了一些参数,或者看不到任何变化,或者破坏了它。

Thanks (again)! 再次感谢)!

Just look at the output: 只看输出:

echo date('Y-1-1', strtotime('-1 year'));
// 2012-1-1

and the documentation: 和文档:

http://php.net/manual/de/function.date.php http://php.net/manual/de/function.date.php

http://php.net/manual/de/function.strtotime.php http://php.net/manual/de/function.strtotime.php

  • Y is the key for the year with four digits. Y是四位数的年份关键字。
  • -1-1 are just normal characters, ignored by date function -1-1只是普通字符,被日期函数忽略
  • -1 year gives the timestamp of now - 1 year, so the year is 2012 -1 year给出了现在的时间戳-1年,所以年份是2012年

That call will return last year's first of January: assuming today's date 2013-12-27 this will give 该电话将在去年1月1日返回:假设今天是2013年12月27日,

  1. strtotime('-1 year') resolves to today's date one year ago, ie 2012-12-27 strtotime('-1 year')解析为一年前的今天,即2012-12-27
  2. everything that date doesn't recognize as a format element is interpreted literally (from the documentation : Unrecognized characters in the format string will be printed as-is. ), so the format string is converted to 2012-1-1 date中不能识别为格式元素的所有内容均按原义进行解释(来自文档Unrecognized characters in the format string will be printed as-is. ),因此格式字符串将转换为2012-1-1

The strtotime function returns a unix timestamp after parsing the supplied string, which in this example gives us the timestamp for the current time, but in the previous year. strtotime函数在解析提供的字符串后返回unix时间戳,在此示例中,该时间戳为我们提供了当前时间(但上一年)的时间戳。

The date function, then, formats this timestamp according to the format: Y-1-1 , which effectively outputs the four digit year in place of Y , and outputs the 1 s literally. 然后, date函数根据以下格式格式化此时间戳: Y-1-1 ,它有效地输出四位数的年份代替Y ,并按字面输出1 s。

So, the complete code: date('Y-1-1', strtotime('-1 year')) returns the string 2012-1-1 if run this year is 2013 , which it is. 因此,如果今年运行的是2013 ,则完整的代码: date('Y-1-1', strtotime('-1 year'))返回字符串2012-1-1

References: 参考文献:

Here is the output from the interactive shell: 这是交互式shell的输出:

~/Code $ php -a
Interactive shell

php > echo strtotime('-1 year');
1356628992
php > echo date('Y-1-1', 1356628968);
2012-1-1
php >

'Y-1-1' basically means the same as 'Current Year-1-1', so it would be 2013-1-1 (1st January of 2013'. Date's function first argument can contain any characters or formatting characters, that might be found here: PHP Date “ Y-1-1”的含义与“当前Year-1-1”的含义相同,因此应为2013-1-1(2013年1月1日)。Date的函数第一个参数可以包含任何字符或格式字符,可能在这里找到: PHP日期

Second parameter is timestamp (UNIX timestamp which is amount of seconds passed from 1st January of 1970). 第二个参数是时间戳(UNIX时间戳,它是从1970年1月1日起经过的秒数)。 strtotime('-1 year') gives you timestamp in 1 year before this year. strtotime('-1 year')为您提供今年前一年的时间戳。

The result would be '2012-1-1'. 结果将是“ 2012-1-1”。 This code listing basically gives you 1st January of previous year as result. 此代码清单基本上将给您去年的1月1日作为结果。

date('Y-1-1', strtotime('-1 year')); // 2012-1-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM