无法获得帖子的时间戳以保持正确发布

时间:2013-11-15 20:39:20

标签: php

我正在尝试为人们在我的简单博客网站上发布的帖子加盖时间戳。我只是希望时间戳位于帖子的底部。我现在正在使用它,但是一旦我发布新帖子,最近帖子的所有时间戳都会更新到新帖子的时间戳。不知道如何解决这个问题,我尝试了不同的php时间戳等,但仍然没有去。在此先感谢帮助我!

这是我的PHP

 <!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="post.css" />

<meta name="viewport" content="width=device-width" />
<title>Daily Dorm News</title>
</head>

<body>
<h1>Your Daily Dorm News Post! </h1>
<div id="container"> <?php if ( isset($_GET['name']) and preg_match("/^[A-Za-z0-9]+$/", $_GET['name']) ) {

    echo $_GET['name'];

} else {

    echo "You entered an invalid name!\n";

}

?><br>


Your email address is: <?php if ( isset($_GET['email']) and preg_match("/.+@.+\..+/i", $_GET['email']) ) {

    echo $_GET['email'];

} else {

    echo "You didn't enter a proper email address!\n";

}
?><br>
You Posted : <?php if ( isset($_GET['message']) and preg_match("/^[A-Za-z0-9]+$/", $_GET['message']) ) {

    echo $_GET['message'];

} else {

    echo "The message is not valid! The message box was blank or you entered invalid symbols!\n";

}
?>

This event happened :<?php echo $_GET["date"]; ?><br>


</div>
<?php 
/* [INFO/CS 1300 Project 3] index.php 
 * Main page for our app.
 * Shows all previous posts and highlights the current user's post, if any.
 * Includes a link to form.php if user wishes to create and submit a post.
 */ 

require('wall_database.php');

// Fetching data from the request sent by form.php  
$name = strip_tags($_REQUEST['name']);
$email = strip_tags($_REQUEST['email']);
$message = strip_tags($_REQUEST['message']);
$date = strip_tags($_REQUEST['date']);

$is_valid_post = true;
// Checking if a form was submitted
if (isset($_REQUEST['name'])){
  // Fetching data from the request sent by form.php  
$name = strip_tags($_REQUEST['name']);
$email = strip_tags($_REQUEST['email']);
$message = strip_tags($_REQUEST['message']); 
$date = strip_tags($_REQUEST['date']);  
  // Saving the current post, if a form was submitted
  $post_fields = array();
  $post_fields['name'] = $name;
  $post_fields['email'] = $email;
  $post_fields['message'] = $message;
  $post_fields['date'] = $date;
  $success_flag = saveCurrentPost($post_fields);

}

//Fetching all posts from the database
$posts_array = getAllPosts();

require('header.php');
?>
<?php date_default_timezone_set('America/New York'); $date_posted = date('h:i:s Y-m-d'); ?>
    <p><a href="form.php">Submit a Post</a></p>

    <?php
    if(isset($name)) {
      echo "<h3>Thanks ".$name." for submitting your post.</h3>";
    }
    ?>

    <p>Here are all the posts we have received.</p>
    <ul id="posts_list">
    <div id="posts">
    <?php 

    // Looping through all the posts in posts_array
    $counter = 1;

    foreach(array_reverse($posts_array) as $post){
      $name = $post['name'];
      $email = $post['email'];
      $message = $post['message'];
      $date = $post['date'];

      if ($counter % 2==1)
        $li_class = "float-left";
      else
        $li_class = "float-right";

      echo '<div class=post>';
  echo '<li class="'.$li_class.'"><h3><span>'.$name.'</span> wrote a post.</h3></li>';
  echo '<li class="'.$li_class.'"><h3><span>'.$name.' email is: '.$email.'</span></h3></li>';
  echo '<li class="'.$li_class.'"><h3><span>'.$name.' wrote '.$message.'</span> wrote a post.</h3></li>';
  echo '<li class="'.$li_class.'"><h3><span>This evemt occured on '.$date.'</span></h3></li>';
  echo $date_posted;
  echo date('l jS \of F Y h:i:s A');


  echo '</div>';
    }
    ?>
    </ul>
  </div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

除非覆盖,否则日期函数默认为当前时间戳。

//echo date('l jS \of F Y h:i:s A');
echo date('l jS \of F Y h:i:s A', strtotime( $date ));

答案 1 :(得分:0)

您未向日期功能提供帖子上的日期。如果您没有将帖子上的日期作为第二个参数提供,那么它每次只会打印今天的日期。如果你的日期没有存储,你需要首先使用strtotime()进行转换。

date_default_timezone_set('EST');
echo date('l jS \of F Y h:i:s A', strtotime($date));