'Date column is calculated to a number and mentioned as a string

In my database I have a date column (type = date). On of the values is this column is '2022-04-29'. When I try to get this date I get an error "Uncaught Error: Call to a member function format() on string".

The code I use:

if ($vrijedag_result = mysqli_query($conn, "SELECT * FROM vrijedagen WHERE datum BETWEEN '$VrijeDagenDatum1' AND '$VrijeDagenDatum2'")) {
    if (mysqli_num_rows($vrijedag_result) > 0) {
        $vrijedagArray = array();
        while ($vrijedagenRij = mysqli_fetch_array($vrijedag_result)) {
            $vrijedagdatum = $vrijedagenRij['datum'] -> format('Y-m-d');
            array_push($vrijedagArray, "" . $vrijedagdatum . "");
        }
    }
    else {
        $vrijedagArray = "";
    }
    mysqli_free_result($vrijedag_result);
}

In my database the columntype is 'date' and the value I see in PHPMyAdming is 2022-04-29. This value is written to my DB via another php script.

When I try to see the value I receive from my script (using JavaScript Alert) I get '1989'...which is a total of 2022 minus 4 minus 29...

Can somone explane what I'm doing wrong? Does someone have a solution? Thanks in advance!

Edit 1: The error comes on the line '$vrijedagdatum = $vrijedagenRij['datum'] -> format('Y-m-d');'



Solution 1:[1]

You should replace this line:

$vrijedagdatum = $vrijedagenRij['datum'] -> format('Y-m-d');

With:

$date = new DateTime($vrijedagenRij['datum']);

$vrijedagdatum = $date->format('Y-m-d');

Or with:

$vrijedagdatum = date_format(date_create($vrijedagenRij['datum']), 'Y-m-d');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dori Rina