php日期函数_PHP日期函数

php日期函数

After include(), date() is the first function that developers respond to with “Oh, that’s why I would use PHP!”

include()date()是开发人员首先响应的函数“哦, 这就是为什么我要使用PHP!”

Before we get to the function itself, a small reminder: on the whole, HTML and CSS are stateless, which means (among other things) that they have no concept of time. The only way to place today’s date on a webpage using HTML is to physically write it out on the page, mark up the code, and upload it to the server… each and every day.

在介绍函数本身之前,需要提醒一下:总体而言, HTMLCSS是无状态的,这意味着(除其他事项外)它们没有时间概念。 使用HTML在网页上放置今天的日期的唯一方法是每天在页面上物理地写出来,标记代码,然后每天上传到服务器。

With PHP, placing the current day of the week on the page couldn’t be easier:

使用PHP,将一周中的当前日期放在页面上再简单不过了:

<p>Today is <?php echo date('l'); ?>

(That’s a lowercase L used inside single quotes for the date function.)

(这是小写的L,用于date函数的单引号中。)

This piece of code demonstrates several important points:

这段代码演示了几个要点:

  1. PHP is, generally speaking, used for dynamic data, i.e. content that changes. In this example, we always want our paragraph to start with the words “Today is”, so it makes little sense to place those words inside a PHP statement. The only thing that changes is the actual name of the day, so that part is PHP.

    一般来说,PHP用于动态数据,即变化的内容。 在此示例中,我们始终希望我们的段落以单词“ Today is”开头,因此将这些单词放在PHP语句中几乎没有意义。 唯一更改的是日期的实际名称,因此部分是PHP。

  2. echo can be used to print out strings, the value of variables, or the result of functions.

    echo可用于打印字符串,变量值或函数结果。

  3. It is important that there is a space somewhere between “is” and the day of the week, otherwise the text will run together.

    重要的是“ is”和星期几之间必须有空格,否则文本将一起运行。
  4. The date() function works with “switches”: individual letters placed into the function to generate date components.

    date()函数与“ switchs”一起使用:将单个字母放入函数中以生成日期成分。

date() can also be used to generate the time. A complete list of switches can be found at the php.net website entry for date(), but for now let’s use an example:

date()也可以用来生成时间。 完整的开关列表可以在php.net网站的date()条目中找到,但现在让我们使用一个示例:

<p>It is now <?php echo date('g:i A')." on ".date('l j F, Y'); ?>

Using concatenation, the result is a much more verbose readout of the current day and time : “It is now 3:07 am on Monday 31 December 2014”.

使用串联 ,结果是当前日期和时间的详细信息:“现在是2014年12月31日星期一凌晨3:07”。

问题 (Questions)

  1. When the date() function is used, it reports the time and date of what, from where?

    date()被使用的功能,它报告什么样的时间和日期,来自哪里

  2. The reported date and time will remain unchanged on the page once it is loaded. After a minute has passed, what would we have to do to show an updated time on the page?

    加载后,报告的日期和时间在页面上将保持不变。 一分钟后,我们该怎么做才能在页面上显示更新的时间?

答案 (Answers)

  1. PHP is a server-side scripting language, so by default it reports the local date and time of the server. This is actually a good thing: servers are at a known place, and keep regular, dependable track of time, whereas clients can and will have any time they wish, which will sometimes be erroneous.

    PHP是服务器端脚本语言,因此默认情况下它报告服务器的本地日期和时间。 这实际上是一件好事:服务器位于已知的位置,并保持规律,可靠的时间跟踪,而客户端可以并且会拥有他们希望的任何时间,这有时是错误的。

  2. In order to update the time and date we would have to physically refresh the page. PHP is a server-side technology: it only works after communication with the server. By refreshing the page we are doing the equivalent of sending the dumbwaiter used in our earlier analogy back downstairs with an order to “give me something new”. It would be massive overkill to have to do this every minute in order to have a counting clock on the page: if you wanted a ticking clock on a web page it would make much more sense to write it in , which works client-side.

    为了更新时间和日期,我们必须物理刷新页面。 PHP是一种服务器端技术 :仅在与服务器通信后才能工作。 通过刷新页面,我们等效于将我们较早的类比中使用的dumbwaiter发送回楼下,以“给我一些新东西”。 为了在页面上有一个计数时钟而必须每分钟执行一次,这将是非常大的矫over过正:如果您想在网页上滴答时钟,那么用编写它会更有意义,它可以在客户端工作

结合使用date()和 (Using date() with includes)

The date() function can also be used in includes. Oddly, you do not have to change the extension of the include file to do so. (This means that any PHP that you use on the included file won’t be color-coded correctly in programs like , but that’s not a major issue. You don’t have to change the extension as any PHP in an included file is run after it is called into a PHP page.)

date()函数也可以在include中使用 。 奇怪的是,您不必更改包含文件的扩展名即可。 (这意味着您在包含文件上使用的任何PHP在诸如程序中都不会正确进行颜色编码,但这不是主要问题。您不必更改扩展名,因为包含文件中的任何PHP都是被调用进入PHP页面运行。)

For example, let’s say you have a copyright notice at the bottom of every page of your website. Having thought ahead, you have decided to make this portion of code an include file, with the filename footer.html. In this file is the following code:

例如,假设您在网站每个页面的底部都有一个版权声明。 经过深思熟虑后,您决定将这部分代码包含为文件名footer.html的包含文件。 此文件中的以下代码:

<h4>Copyright &copy; 2010 Grievous Genophage Inc.</h4>

Your client is copyright-obsessed, and wants the copyright notice updated every year. Rather than using New Year’s to update the website every time, you can modify footer.html to become:

您的客户痴迷于版权,并且希望每年更新版权声明。 您可以将footer.html修改为:而不是每次使用“新年”来更新网站。

<h4>Copyright &copy; <?php echo date('Y'); ?> Grievous Genophage Inc.</h4>

翻译自: https://thenewcode.com/178/The-PHP-date-function

php日期函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值