PHP date() return 1969-12-31

Issue

I am trying to develop a system that checks if a date has already passed. So I take the string from mysql, convert it to date and compare it. The problem is that date('Y-m-d', $date) returns the UNIX release date that is 1969-12-31 (an incorrect date).

This is my code:

    $row = $result->fetch_assoc(); // fetch data   
    $date = $row['date'];
    $time = strtotime($date);
    $newformat = date('Y-m-d', $date);

    echo $newformat . " " . $row['date'];


    $today = date('Y-m-d');
    if($newformat > $today){
        header('Location: hhtps://bouncerbot.go-atcode.com');
    }

row['date'] is not null since I have already tried to output and it returns the correct date (in the form of a string) which is in the row.

Where could the error be?

Solution

Replace $date with $time variable.

The $date value is coming from your DB query as a date string (‘2021-12-19’), but the PHP date() function requires a timestamp value for the 2nd parameter.

$newformat = date('Y-m-d', $date);  // switch this to below
$newformat = date('Y-m-d', $time); 

Answered By – swtweb

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published