drpjdfj618393 2017-06-10 13:41
浏览 25
已采纳

在php中显示mysql的日期

So i want to get one and only the first value from a date column in my database, and put it in my PHP file, this is the snippet of the code to get the date:

            $one = 1;
            $Lihat="SELECT * FROM event where status = '$one'";
            $Tampil = mysqli_query($db, $Lihat);
            while ( $hasil = mysqli_fetch_array ($Tampil)) {
                    $query_start_date = "SELECT tgl_event
                                    FROM jadwal_acara
                                    WHERE id_event = '$id_event' AND status = $one
                                    ORDER BY tgl_event ASC
                                    LIMIT 1";

                $result = mysqli_query($db, $query_start_date);
                $start_date = mysqli_fetch_field($result);

     <td class="tgl_mulai"><?php echo date('d-m-Y', strtotime($start_date));?></td>

The problem is that when i tried to echo it in my table row:

It returns an error that said:

strtotime() expects parameter 1 to be string, object given. 

Does anyone know how to change it into a string or another method to get the desired display result? Thank you.

EDIT:

$start_date = var_dump($start_date); returns a NULL value when i tested it.

Notice: Undefined property: stdClass::$tgl_event in NULL

But i got other rows that displayed the correct name and event ID, it's just the date that won't display correctly.

it now displays :01-01-1970

  • 写回答

1条回答 默认 最新

  • dtiu94034 2017-06-10 14:07
    关注

    mysqli_fetch_field() returns an object. Instead use mysqli_fetch_assoc

    Try this:

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    $query = "SELECT tgl_event FROM jadwal_acara WHERE id_event = ".$id_event." AND status = 1 ORDER BY tgl_event ASC LIMIT 1";
    
    if ($result = mysqli_query($db, $query)) {
        while ($finfo = mysqli_fetch_assoc($result)) {
    
        $date = date('m-d-Y', strtotime($finfo['tgl_event']));
        }
        mysqli_free_result($result);
    }
    
    mysqli_close($db);
    

    And then in your HTML:

    <td class="tgl_mulai"><?php echo $date; ?></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛