Search code examples
phphtmllinuxdatetimecomments

PHP Comment Section With Time Entry


I'd like to make a comment box with tome entry. Like for example
John S. : This is a comment.
Sent: 3:24PM
So here is my code. Can someone help me with this?

<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 );
  }
  ?>


Solution

  • Add this line:

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

    after the comment, so before fwrite( $file, "<br><hr>" );line.

    What this outputs is something like this:

    Sent: 09:23PM<br>
    

    If you need a different format then check the date documentation https://www.php.net/manual/en/function.date.php - its format parameter.