首页 > 解决方案 > 带时间输入的 PHP 注释部分

问题描述

我想用大部头条目制作一个评论框。例如
John S. :这是一条评论。
发送时间:下午 3:24
所以这是我的代码。有人可以帮我弄这个吗?

<form method="post">
        <textarea name="txt" cols="25" rows="5" placeholder="Type here your comment!" required="required"></textarea>
        <br><input type="submit" value="Submit" name="submit" />
<?php
        if ( isset( $_POST[ 'submit' ] ) ) {
         $com  = $_POST[ "txt" ];
          $file = fopen( "inrg.txt", "a" );
         fwrite( $file, "<br> <font color=\"#44b9df\"><em>User:</em></font> " );
         for ( $i = 0; $i <= strlen( $com ) - 1; $i++ ) {
              fwrite( $file, $com[ $i ] );
              if ( $i % 37 == 0 && $i != 0 ) fwrite( $file, "<br/>" );
         }
            fwrite( $file, "<br><hr>" );
            fclose( $file );
        }
        ?>
        <br>
        </form>
    </div>
    <div class="commentcard"><a name="comments">
<font face="Times New Roman"><b><p class="commentcimsor">Comments: </p></b></font>
<font face="Comic Sans MS" color="#000" size="4"><div class="commentscroll">
  <?php
  if (file_exists("inrg.txt")) {
  $file = fopen( "inrg.txt", "r" );
  echo fread( $file, filesize( "inrg.txt" ) );
  fclose( $file );
  }
  ?>

标签: phphtmllinuxdatetimecomments

解决方案


添加这一行:

fwrite($file, "Sent: ".date('h:iA')."<br>");

在评论之后,所以fwrite( $file, "<br><hr>" );在行之前。

这个输出是这样的:

Sent: 09:23PM<br>

如果您需要不同的格式,请查看日期文档https://www.php.net/manual/en/function.date.php - 它的格式参数。


推荐阅读