简体   繁体   中英

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. 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. Still advisable to use the standard open/close tags because of older PHP versions that are not viable at understanding the newer tags.

So my question in short is : How come the DateTime class at the echo statement only accepts <?= ?> this tag to show me some output.

<?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(...

Note that this tag is not to be confused with the short open tag: <? - 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

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 

$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>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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