简体   繁体   中英

Displaying date from mysql in php

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

mysqli_fetch_field() returns an object. Instead use mysqli_fetch_assoc

Try this:

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM