0

I am a beginner in PHP and Javascript, so I'm struggling to compare two dates in a SELECT request like this:

<?php
require 'db.php';
$sql = "SELECT * FROM shortcode WHERE invalid_from_shortcode < Date()";
$result = pg_query($sql);

if (!$result) {
    echo "Problem with query " . $query . "<br/>";
    echo pg_last_error();
    exit();
}
?>

I'm trying to select only the 'shortcodes' where the [invalid_from_shortcode] column is inferior from the current date.
I don't know how to get the current date and whether the structure is correct.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

6 Answers6

3
<?php
  require 'db.php';
  $sql = "SELECT * FROM shortcode WHERE invalid_from_shortcode < NOW()";
    $result = pg_query($sql);
    if (!$result) {
        echo "Problem with query " . $query . "<br/>";
        echo pg_last_error();
        exit();
    }
 ?>

Date() is a php function. in Mysql/PostgreSQL you must use NOW()

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

You want to use NOW() not DATE().

Cuagau
  • 548
  • 3
  • 7
0

May be you are looking for CURDATE() .

 $sql = "SELECT * FROM shortcode WHERE DATE(invalid_from_shortcode) < CURDATE()";
milan kumar
  • 226
  • 2
  • 16
0

use

NOW()      Returns the current date and time
Luca
  • 344
  • 5
  • 18
0

You should be using NOW() function instead of date() and you need to convert the date in a suitable format enter link description here you can visit this link for converting the date format

Parvez Khan
  • 28
  • 1
  • 4
0
...
$sql = "SELECT * FROM shortcode WHERE invalid_from_shortcode < Date()"; 
...

A PHP functions like Date() is not visible in the SQL string. Either pass a value or use an equivalent Postgres function, like @pr1nc3 already suggested.

However, while you do not disclose the actual data type of invalid_from_shortcode it's unclear whether now() can serve as equivalent function. It returns the current timestamp with time zone, which is meaningfully different from the current date - effectively equivalent to the current timestamptz truncated to 00:00 according to the current timezone setting. You might need CURRENT_DATE (same as now()::date) instead. Related:

Aside: Unlike two other answers suggested, there is no CURDATE() in Postgres. That probably confusing MySQL with Postgres.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228