user9277271
user9277271

Reputation:

PHP Uncaught Error: Class 'date_format' not found

I faced this error message:

Fatal error: Uncaught Error: Class 'date_format' not found in func.php on line 20

And here is the function in func.php:

function get_comments(){
$get = "SELECT * FROM `parents` ORDER BY `date` DESC";
$result2 = mysqli_query($GLOBALS['con'],$get);
$row_cnt = mysqli_num_rows($result2);
foreach($result2 as $item){
    $date = new dateTime($item['date']);
    $date = new date_format($date,'M j, Y | H:i:s');
    $user = $item['user'];
    $comment = $item['text'];
    $par_code = $item['code'];

    echo "
        <div class='comment' id='$par_code'>
        <p class='user'>$user</p>
        <p class='time'>$date</p>
        <p class='comment-text'>$comment</p>
        <a class='link-reply' id='reply' name='$par_code'>Reply</a>
    ";

    $chi_result = mysqli_query($con,"SELECT * FROM `children` WHERE `par_code`='$par_code' OREDER BY `date` DESC");
    $chi_cnt = mysqli_num_rows($chi_result);

    if($chi_cnt == 0){

    }else{
        echo "
            <a class='link-reply' id='children' name='$par_code'><span id='tog_text'>replies</span>$chi_cnt</a>
            <div class='child-comments' id='C-$par_code'
        ";

        foreach($chi_result as $com){
            $chi_date = new dateTime($com['date']);
            $chi_date = new date_format($chi_date,'M j, Y | H:i:s');
            $chi_user = $com['user'];
            $chi_com = $com['text'];
            $chi_par = $com['par_code'];

            echo "
                <div class='child' id='$par_code'-C>
                    <p class='user'>$chi_user</p>
                    <p class='user'>$chi_date</p>
                    <p class='user'>$chi_com</p>
                </div>
            ";
        }
        echo "</div>";
    }
    echo "</div>";
}

}

And line 20 is this:

$date = new date_format($date,'M j, Y | H:i:s');

So what is wrong here and how to fix this problem?

Thanks in advance...

Upvotes: 0

Views: 5316

Answers (3)

Sebastian Brosch
Sebastian Brosch

Reputation: 43584

The date_format is not a class it is a function. So you can't use new here to create an object.

The date_format is a alias of the method DateTime::format. You can find the following examples on the PHP documentation. These examples show how you can use the date_format as function and the method (format):

//using as method of the DateTime object.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');

//using as function (the first parameter is the DateTime object).
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');

So you have two possibilities:

solution #1 (using function date_format):

$chi_date = new DateTime($com['date']);
$chi_date = date_format($chi_date, 'M j, Y | H:i:s');

//also possible a one-liner
$chi_date = date_format(new DateTime($com['date']), 'M j, Y | H:i:s');

solution #2 (using method format):

$chi_date = new DateTime($com['date']);
$chi_date = $chi_date->format('M j, Y | H:i:s');

//also possible a one-liner
$chi_date = (new DateTime($com['date']))->format('M j, Y | H:i:s');

Upvotes: 1

Neetesh
Neetesh

Reputation: 917

Write

$date = date('d/m/Y h:m:s a', time());

instead if you want to take date with time

or

See here for more options, https://www.w3schools.in/php/date-time/

Upvotes: 0

axiac
axiac

Reputation: 72226

date_format() is a function, not a class. It is, in fact, an alias of DateTime::format(). Your code should read:

        $date = new DateTime($com['date']);
        $chi_date = $date->format('M j, Y | H:i:s');

Don't store two different, unrelated values, in the same variable (as your current code puts in $chi_date) because it is confusing for the reader.

Upvotes: 1

Related Questions