Inserting Existing Date Parameters from SQL into PHP (with formatting)

31 views Asked by At

I have a PHP function which inserts a date value from my SQL database into my PHP page here

$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);

if ($DateResultCheck > 0){
    while($row = mysqli_fetch_assoc($DateResult)){
        echo "<p style=\"margin-left: 15px;\">{$row[('insider_date')]}</p>";
    }
}

This method works fine for printing out the date like this

2020-01-31

But I want the date data to print out as "January 31, 2020". Would anyone know how to output the data like this?

1

There are 1 answers

0
Talk Nerdy To Me On BEST ANSWER

There's a couple ways to do date formatting in PHP. Here I use the DateTime::format() method:

$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);

if ( $DateResultCheck > 0 ) {
    while ( $row = mysqli_fetch_assoc($DateResult) ) {
        $date = new DateTime( $row['insider_date'] );
        echo '<p style="margin-left: 15px;">' . $date->format('F j, Y') . '</p>';
    }
}