简体   繁体   English

PHP DateTime类打开/关闭标签

[英]PHP DateTime Class opening/closing tags

I am following some tutorials and came across something i can't seem to wrap my head around. 我正在关注一些教程,遇到了一些似乎无法解决的问题。 Been searching for the reasoning or meaning of it. 一直在寻找其推理或含义。

Just practicing with the DateTime class i came across this code and tried it out. 刚练习DateTime类时,我遇到了这段代码并进行了尝试。 The code won't give me output when i use. 使用时,代码不会给我输出。

<?php

it does give me output when i use. 当我使用时它确实会给我输出。

<?=

I've read about this kind of notation in PHP and it's more a personal preference nowadays. 我已经在PHP中阅读了这种表示法,如今它已成为一种个人偏好。 Still advisable to use the standard open/close tags because of older PHP versions that are not viable at understanding the newer tags. 仍然建议使用标准的打开/关闭标签,因为较旧的PHP版本无法理解较新的标签。

So my question in short is : How come the DateTime class at the echo statement only accepts <?= ?> this tag to show me some output. 所以我的问题简而言之是:echo语句中的DateTime类为什么只接受<?= ?>这个标签来显示一些输出。

<?php

$publishDate = '2014-08-24 09:14:00';

$localDateTime = new DateTime($publishDate, new DateTimeZone('America/New_York'));

$utcDateTime = clone $localDateTime;

$utcDateTime->setTimeZone(new DateTimeZone('UTC'));

?>

<p>The UTC date/time is: <?= $utcDateTime->format("Y-m-d H:i:s") ?></p>
<p>The New York date/time is: <?= $localDateTime->format("Y-m-d H:i:s") ?></p>"

<?= is a shorthand PHP echo statement, essentially meaning <?php echo(... <?=是PHP echo语句的简写,本质上是<?php echo(...

Note that this tag is not to be confused with the short open tag: <? 请注意, 请勿将此标签与short open标签混淆: <? - it is a completely different operator and, as of PHP 5.4, the "short echo tag" is actually not affected by the short_open_tag setting: http://php.net/manual/en/language.basic-syntax.phptags.php -它是完全不同的运算符,从PHP 5.4开始,“ short echo标签”实际上不受short_open_tag设置的影响: http : //php.net/manual/zh/language.basic-syntax.phptags.php

So <?php and <?= are two different things, the latter is a shorthand that also uses echo if you wanted to achieve the same thing with <?php you would do something like: 所以<?php<?=是两个不同的东西,后者是一个速记,如果您想用<?php实现相同的事情,它也使用echo ,您将执行以下操作:

<?php 

$date = new DateTime();
echo $date->format('Y-m-d H:i:s');

Or in your example: 或在您的示例中:

<p>The UTC date/time is: <?php echo $utcDateTime->format("Y-m-d H:i:s") ?></p>
<p>The New York date/time is: <?php echo $localDateTime->format("Y-m-d H:i:s") ?></p>"

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

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