Alvaro Parra
Alvaro Parra

Reputation: 805

SELECT DATE_FORMAT(NOW(),'%m-%d-%Y') in php is not working well

can anyone help me with this:

$querydate ="SELECT DATE_FORMAT(NOW(),'%m-%d-%Y') AS dat";
$query = mysql_query($querydate);
$row = mysql_fetch_assoc($query);
$fecha =  $row['dat'];

-2013-31+07 it is returning -2037

And i want it to return today's date

31-07-2013

Upvotes: 2

Views: 12604

Answers (3)

Gigline
Gigline

Reputation: 297

There could be reasons for pulling the date that aren't apparent in your question, but if your MySQL implementation is on the same machine as your php code then you could get the results your looking for by running:

$fecha = date("d-m-Y");

And save yourself the database call.

Upvotes: 0

spencer7593
spencer7593

Reputation: 108450

Given your code example, a value of '07-31-2013' should be assigned to fecha;

If you want the day before the month, then change the format string to '%d-%m-%Y'.


Evaluated in a numeric context, the expression -2013-31+07 should return -2037. That is expected behavior.

But we don't see in your code where this evaluation is taking place.

Upvotes: 0

Erman Belegu
Erman Belegu

Reputation: 4079

Try this

SELECT DATE_FORMAT(NOW(),'%d-%m-%Y') AS dat

Here you have more info for the format of date

Upvotes: 2

Related Questions