日期函数无法使用bind_result

时间:2015-08-21 18:10:43

标签: php mysql function date datetime

我的文件中有这种日期格式。如果我执行正常的SELECT查询,它会很有效,但是当我执行一个没有bind_param的预准备语句查询并为其分配bind_result变量时,我无法使其工作

这是功能。它在美国中部时间显示。我怎样才能到达东部时间?

function fixDate($strDateTime) {

 $strFormat = 'M, j, Y';
    $strFormatTime = '\a\t g:ia';
    $intTimeStamp = strtotime($strDateTime);
    $strDate = date($strFormat, $intTimeStamp);
    $strTime = date($strFormatTime, $intTimeStamp);

    if($strDate == date($strFormat)) {
        return "Today " . $strTime;
    }
    elseif($strDate == date($strFormat, strtotime('yesterday'))) {
        return "Yesterday " . $strTime;
    }
    else {
        return " on " . $strDate . " " . $strTime;
    }
}

如果我使用正常SELECT查询执行此操作,该函数将起作用。

$date = $row2['topic_date'];
$date = fixDate($date);

但如果我有$date的bind_result并尝试执行此操作

$date = fixDate($date);

它无效。

如何使用此函数处理预准备语句的bind_result变量?

更新:

这有效..

$query2 = mysqli_query($con,"SELECT * FROM forum_topics
    INNER JOIN forum_categories ON
    forum_topics.category_id = forum_categories.id
    INNER JOIN users
    ON forum_topics.topic_creator = users.id
    ORDER BY forum_topics.topic_reply_date DESC
    LIMIT 3")
    or die ("Query2 failed: %s\n".($query2->error));
    $numrows2 = mysqli_num_rows($query2);
    if($numrows2 > 0){

    $topics .= "<table class='top_posts_table'>";
    $topics .= "<tr><th class='top_posts_th'>Topic Title</th><th class='top_posts_th'>Replies</th><th class='top_posts_th'>Views</th></tr>";
    $topics .= "<tr><td colspan='3'><hr /></td></tr>";
    while($row2 = mysqli_fetch_assoc($query2)){
        $cid = $row2['cid'];
        $tid = $row2['id'];
        $title = $row2['topic_title'];
        $views = $row2['topic_views'];
        $replies = $row['tid2'];
        $date = $row2['topic_date'];
        $date = fixDate($date);
        $creator = $row2['username'];
        $topics .= "<tr><td class='top_posts_td'><a href='forum_view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted 
        by: ".$creator."<br>".$date."</span></td><td class='top_posts_td'>0</td><td class='top_posts_td'>".$views."</td></tr>";
        $topics .= "<tr><td colspan='3'><hr /></td></tr>";

这不起作用......

  if ($announcements_stmt = $con->prepare("SELECT announcements.id, announcements.user_id, announcements.message, announcements.date, users.username FROM announcements
                        INNER JOIN users
                        ON announcements.user_id = users.id")) {

    $announcements_stmt->execute();
    $announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date, $announcements_username); 

    if (!$announcements_stmt) {
        throw new Exception($con->error);
    }
 }   

$announcements_stmt->store_result();
         $announcements_result = array();
            $announcements_date = $announcements_date;
            $announcements_date = fixDate($announcements_date);
?>
                <div class="index_announcements_out">
                    <div id="announcements_title">League Announcements:</div>
                    <div class="index_announcements_wrap">
                        <table class="index_announcements_table">   
<?php
        while ($row = $announcements_stmt->fetch()) {
?>
                            <tr class="index_announcements_border">
                                <td class="index_announcement_pic"></td>
                                <td class="index_announcement_username_td">FROM: <?php echo $announcements_username; ?><br>on <?php echo $announcements_date; ?></td>
                                <td class="index_announcement_message_td"><?php echo $announcements_messages; ?></td>
                            </tr>

1 个答案:

答案 0 :(得分:1)

基本上,在使用bind_result()时,您需要在变量填充之前执行fetch()。在这种情况下,您会在发生这种情况之前尝试访问它们。

在给定的代码中,没有必要在while循环之外获取固定的$ announcements_date,因此只需在循环中用<?php echo $announcements_date; ?>替换<?php echo fixDate($announcements_date); ?>即可。