2

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
Erman Belegu
  • 4,074
  • 25
  • 39
Alvaro Parra
  • 796
  • 2
  • 8
  • 23
  • 2
    If you want 31-07, then it should be `%d-%m-%Y` for the format. d = day, m = month, Y = 4-digit year. I can't see how this could return a negative year. Are you sure that `-` on the -2037 is coming from the DB, and not something else in your code? – Marc B Jul 31 '13 at 21:19
  • You shouldn't be using mysql_* function in new development, switch to mysqli or pdo ... more information [here](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189) – Orangepill Jul 31 '13 at 21:20

3 Answers3

2

Try this

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

Here you have more info for the format of date

Erman Belegu
  • 4,074
  • 25
  • 39
0

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.

spencer7593
  • 106,611
  • 15
  • 112
  • 140
0

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.

Gigline
  • 297
  • 1
  • 4