douyunjiaok300404 2019-06-07 07:26
浏览 63
已采纳

在为echo命令调用列时打印出错误的日期

I am trying to show only the year from a date field for my loop, but when I am trying to call it, it shows me the wrong year. It shows 1970 for all the results.

I'm new to wordpress and php and don't know how to exactly show my results. Is my query for $mydate incorrect or is it a coding error when being echoed?

<table class="teachpress_publication_list">

      <?php

        global $wpdb;

    // QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION 
    $total = $wpdb->get_var("SELECT COUNT(*) FROM (SELECT * FROM wpfs_teachpress_pub LIMIT 0,650) AS a");
    $post_per_page = 10;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $offset = ( $page * $post_per_page ) - $post_per_page;
    $result = $wpdb->get_results( "SELECT * FROM wpfs_teachpress_pub ORDER BY date DESC LIMIT $post_per_page OFFSET $offset");
    $mydate = $wpdb->get_results( "SELECT date FROM wpfs_teachpress_pub" );
    foreach ( $result as $print )   { ?>

          <tr class="tp_publication">

                  <td class="tp_pub_info"> <p class="tp_pub_author"> <?php echo $print->editor; ?><br/>
                  <span class="tp_pub_title"> <?php echo $print->title; ?></span><br/><?php echo $print->journal; ?>&nbsp;<span class="dd">Year Published:<?php echo date("Y", strtotime($mydate)); ?></span> </p>

          </tr>

            <?php }
          ?>
</table>

I expect the output to show the correct year in the date column of MySql db instead of always 1970.

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • donglu6805 2019-06-10 08:52
    关注

    Finally figured it out just used print function along with date() function

    <?php echo date("Y", strtotime($print->date)); ?>
    

    Now am able to display only year from my db column.

    Thanks a lot all for the support!

    Hariharan

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗