0

Hey everyone,

Sorry if these files seem familar. I have an issue with a certain date that won't change along with the rest. For example: "Sun., Sep 16th, 2012 6: 26 PM" that's the way the code is supposed to read but there is one section that won't change and I can't seem to figure out what's wrong. I've looked and looked over my code files and can't figure it out. Help! and Thanks in advance!

///this is the line of code where the error starts.////
<p>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?>

///Here is the file in PHP with sql that determines the date.////
<?php

//checks if the given post id is in the table
function valid_pid($pid) {
    $pid = (int)$pid;

    $total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = {$pid}");
    $total = mysql_result($total, 0);

    if ($total != 1) {
        return false;
    }else{
        return true;
    }
}

//gets a summary of all blog posts
function get_posts() {
    $sql = "SELECT
                `posts`.`post_id` AS `id`,
                `posts`.`post_title` AS `title`,
                LEFT(`posts`.`post_body`, 512) AS `preview`,
                `posts`.`post_user` AS `user`,
                DATE_FORMAT(`posts`.`post_date`, '%a., %b %D, %Y %l: %i %p') AS `date`,
                `comments`.`total_comments`,
                DATE_FORMAT(`comments`.`last_comment`, '%a., %b %D, %Y %l: %i %p') AS `last_comment`
            FROM `posts`
            LEFT JOIN(
                SELECT
                    `post_id`,
                    COUNT(`comment_id`) AS `total_comments`,
                    MAX(`comment_date`) AS `last_comment`
                FROM `comments`
                GROUP BY `post_id`
            ) AS `comments`
            ON `posts`.`post_id` = `comments`.`post_id`
            ORDER BY `posts`.`post_date` DESC";

    $posts = mysql_query($sql);

 $rows = array();
        while (($row = mysql_fetch_assoc($posts)) !== false) {
                $rows[] = array(
                        'id'                       => $row['id'],
                        'title'                   => $row['title'],
                        'preview'             => $row['preview'],
                        'user'                   => $row['user'],
                        'date'                   => $row['date'],
                        'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
                        'last_comment'     => ($row['last_comment'] === null) ? 'none' : $row['last_comment']
                );
    }

    return $rows;
}

//gets a single post from the table
function get_post($pid) {
    $pid = (int)$pid;

    $sql = "SELECT
                `post_title` AS `title`,
                `post_body` AS `body`,
                `post_user` AS `user`,
                `post_date` AS `date`
            FROM `posts`
            WHERE `post_id` = {$pid}";

    $post = mysql_query($sql);
    $post = mysql_fetch_assoc($post);

    $post['comments'] = get_comments($pid);

    return $post;
}

//adds a new blog entry
function add_post($name, $title, $body) {
    $name = mysql_real_escape_string(htmlentities($name));
    $title = mysql_real_escape_string(htmlentities($title));
    $body = mysql_real_escape_string(nl2br(htmlentities($body)));

    mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");
}

?>

Thanks!
-GENEH23

4
Contributors
34
Replies
125
Views
4 Years
Discussion Span
Last Post by geneh23
Featured Replies
  • Nevermind! I solved it! I just added the Date_Format() and added the format that I wanted in the "get_post" function. Now it makes sense..the get_post's' function had the right format as well as the comments function..it only makes sense that the "get_post" function have the same..fail on my part lol … Read More

0

I forgot to add what the date that doesn't display properly shows.

Here's what it shows: "2012-09-16 18:26:05"

0

For example: "Sun., Sep 16th, 2012 6: 26 PM" that's the way the code is supposed to read but there is one section that won't change and I can't seem to figure out what's wrong. I've looked and looked over my code files and can't figure it out. Help! and Thanks in advance!

I don't see anything wrong here:

'%a., %b %D, %Y %l: %i %p'

which your output is this:

Sun., Sep 16th, 2012 6:26 PM" 

I don't anywhere in your code that could make this:

Here's what it shows: "2012-09-16 18:26:05"

If this blog has a section that has a old format the only way to fixed that is to change it manually or create a INSERT statement with the date format (correct date format) into the table in your database.

Edited by LastMitch: grammer

0

There must be something wrong with the function that adds the post to the database because I tested the page where I add the post and clicked submit and the the date displayed the same as in the error. I'm guessing the issue deals within the database. How do I fix this?

Thanks for responding by the way! You've always been a help and I appreciate it!

0

I'm guessing the issue deals within the database. How do I fix this?

You can read this:

http://www.fayazmiraz.com/php-mysql-tutorial-how-to-use-mysql-date-correctly-in-php-date-function/

and this:

http://www.ntchosting.com/mysql/insert-date.html#How_to_Insert_a_Date_in_MySQL

This is link shows you the list:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

It's best if you try this not on the blog database but on the separate database so you can learn how to INSERT it correctly. You need to INSERT that query in that table (that is connected to that page) in your database. After that it will show the correct format.

0

This is what the shows in the date field when I press submit on the post blog page: ![85835aef803a8b56bcc382e38ee7cfa0] 85835aef803a8b56bcc382e38ee7cfa0

0

This is what the shows in the date field when I press submit on the post blog page

Did you INSERT the statement? Can you post the query that you INSERT with.

0

This is what is what I originally have in the function, add post but how would I set the query to use the correct current date and time from when the post was posted?

INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())
0

This is what is what I originally have in the function, add post but how would I set the query to use the correct current date and time from when the post was posted?

You need to insert the query in the date column I assume it's the post_date? I'm not sure because I don't know your database structure. Since have it like this:

DATE_FORMAT(`posts`.`post_date`, '%a., %b %D, %Y %l: %i %p') AS `date`

It should look like this also something simliar:

"INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '%a., %b %D, %Y %l: %i %p')";
0

Would it look something like this?:

mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', CURDATE(NOW()))");
0

Would it look something like this?:

I don't know because I don't table structure.

mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");

OK, is this table related to that page? Is this table connected any other pages? If not then try it but if it's connected to other pages then that's not the right query because it will interfere with other pages.

You mention it only that page (section) is not echoing out the date format correctly from the database.

0

Yes this query is only related to that page. Here is my database structure below and I tried using what you posted and nothing happend. e26b943b3702570117e4362755516987

0

out of all this code (in your first post here), also post code which you use to call all that functions, and which pariticular function gives you date in default format.

0

So I realized that the issue is only on one page and everywhere else displays the date correctly so It can't be the post blog function in the code I posted in the beginning of this page. It has to be something within the blog page. That's the only thing that would make sense as to why it's displaying differently on this page rather than multiple ones. Here's the file:

<?php

include('init.php');
include('core/init.inc.php');

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])) {
    if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
        header("Location: blog_read.php?pid={$_GET['pid']}");
    }else{
        header('Location: blog_list.php');
    }

    die();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="" />
<meta name="keywords" content="" />
<title>- Blog -</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="wrapper">
    <?php include 'includes/header.php' ?>
    <div id="menu">
        <?php include 'includes/menu.php' ?>
        <br class="clearfix" />
    </div>
    <div id="splash">
        <img class="pic" src="images/pic01.jpg" width="870" height="230" alt="" />
    </div>
    <div id="page">
        <div id="content"><div class="ic"></div>
            <?php

            if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false) {
                echo 'Invalid post ID.';
            }else{
                $post = get_post($_GET['pid']);

                ?>
                <h2 style="text-decoration: underline;"><?php echo $post['title']; ?></h2>
                <p>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php $user_count = user_count(); echo count($post['comments']); ?> comment<?php $suffix = ($user_count != 1) ? 's' : ''; echo $suffix; ?>)</p>

                <hr />

                <p><?php echo $post['body']; ?></p>

                <hr />
                <h3> There <?php $user_count = user_count(); $suffix2 = ($user_count != 1) ? 'are' : 'is'; echo $suffix2; ?> a total of (<?php $user_count = user_count(); echo count($post['comments']); ?> comment<?php $suffix = ($user_count != 1) ? 's' : ''; echo $suffix; ?>)</h3>
                <?php

                foreach ($post['comments'] as $comment){
                    ?>
                    <p><?php echo '<div style="display: table-cell; padding-left: 5px; padding-right: 5px; background-color: #E0DCDC; -webkit-border-radius: 30px; -moz-border-radius: 30px; border-radius: 30px;">' . $comment['body'] . '</div>'; ?></p>
                    <p>By <?php echo $comment['user']; ?> on <?php echo $post['date']; ?></p>
                    <hr />
                    <?php
                }

                ?><br />
                <h3>Leave a Comment:</h3>
                <form action="" method="post">
                    <p>
                        <label for="user">Name</label>
                        <input type="text" name="user" id="user" />
                    </p>
                    <p>
                        Your comment:<br />
                        <textarea name="body" cols="50" rows="5"></textarea>
                    </p>
                    <p>
                        <input type="submit" value="Add Comment!" />
                    </p>
                </form>
                <?php
            }

            ?>
<br class="clearfix" />
        </div>
        <div id="sidebar">
            <?php include 'includes/sidebar.php' ?>
        </div>
        <br class="clearfix" />
    </div>
    <?php include 'includes/bottom_content.php' ?>
</div>
<?php include 'includes/footer.php' ?>
</body>
</html>

The lines of code that displays the date/time are on lines 47 and 60. Thanks in advance!

0

It has to be something within the blog page. That's the only thing that would make sense as to why it's displaying differently on this page rather than multiple ones. Here's the file:

It's good that you post it but I don't see anything wrong with your code:

<?php echo $post['date']; ?>

The date is in your database.

The reason I mention I don't know your table structure is because I don't know which column or table is being inserting to your page and what other pages are connected to that.

There's nothing that can interfere with your date format from the code you provided.

0

change line 69 in your first post

from

 `post_date` AS `date`

 to 


 DATE_FORMAT(`post_date`, '%a., %b %D, %Y %l: %i %p') AS `post_date`
0

I don't get why this is..

It really does look strange. I think urtrivedi tweak the query. You can test it out.

0

when I change the date_format as suggested, it takes away the date and time all together.

0

when I change the date_format as suggested, it takes away the date and time all together.

This is really weird.

Do you have another table that is simliar to the one you provided?

Another words another table that has a date format.

Can you post that and the code too.

We need to compare the code and table that is working correctly to the code and table you provided here that is not working.

Edited by LastMitch: grammer

0

unfortunatly I don't..

Was this is the original file (the old code) that you posted here?

If not, post the original code (before the changes) so I can compare and see the changes.

Right now, it's just strange that you have 2 different date format on 2 different pages.

It shouldn't happend like that.

0

Well I originally had used "%m-%d-%Y %H:%i:%s" in the original file and it displays what you see on that one page that won't change format. I changed it to "%a., %b %D, %Y %l: %i %p" to display more user friendly to read apposed to the other. Everything else displays correctly except that one page..and it is odd because the code that outputs the date is exactly the same. That's why I figured the issue would be on that specific page. It can't be a database issue because everything displays correctly on the other pages so I'm at a loss..

0

I have an Idea, How would I set the time in the database with the insert into query that I have at the bottom of the origianl file, without changing anything else? for example it says now:

"INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())"
0

nevermind, that wouldnt work because I just remembered that all the other pages were updated correctly so no matter how I change the query, it won't work.

0

Is there another way I could go about this date thing in php and mysql without changing things too much?

0

Is there another way I could go about this date thing in php and mysql without changing things too much?

I'll be honest with you I don't know. There's nothing wrong with your code that you provided. I'm not sure what suggestion I can give you. The reason is that I can't duplicate what you got.

0

use date_format() method example of this is given below.Read manual if you want to change some other format.

    <?php
      echo date("Y-m-d H:i:s");
      echo "<br/>";
    $date = new DateTime(date("Y-m-d H:i:s"));        
    echo date_format($date, 'D, F jS, Y, g:i A');
    ?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.