How do I write the php code to output curdate() from mysql using $pdo

How do I write the php code to output curdate() from mysql using $pdo??
Currently I’m trying

try{
    $sql = 'SELECT CURDATE()';
    $date = $pdo->query($sql);
}
catch (PDOException $e){
    $output = 'Unable to get current date. Check syntax is correct.' . $e->getMessage();
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
    exit(); 
}
echo $date;

But its throwing an error. Any suggestions?

And the reason you’re not using the native php date is???

1 Like

Pretty much this. It’s possible the database server and web server are in different timezones. If you’re connecting to mysql to get the time from the database server, you’re better off just manually setting the correct timezone for a DateTime object. e.g. from the manual:

$date = new DateTime();
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('d/m/Y H:i:s');

Thanks TomB /DaveMaxwell
I should explain a bit more. I’m only using a local testing server. The code I am developing will use curdate() to record any user interaction with the database and the CMS I’m hoping to build will query that date in many ways…e.g.

$result = $pdo->query('SELECT id, userdate FROM users
WHERE userdate >= CURDATE() - INTERVAL DAYOFMONTH(CURDATE()) DAY’);}

So essentially I’m looking to output (in my own testing environment) the value of that clause in bold above. Just to see if its evaluating to what I hope it will.

Hope that explains a bit more. Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.