Domanda

I'm trying to count the days since a post was made. SQL saves the date and time in the table pins in the column time. The format is 2013-11-20 10:36:01.

I have read up on how to do this on http://www.w3schools.com/sql/func_date_format.asp

and have come up with the following code:

<?php 
$dayssince = mysql_query("SELECT DATEDIFF(day,'CURDATE()','$pinDetails->time') AS DiffDate     FROM pins WHERE id ='$pinDetails->id'");
?>
<?php echo $dayssince; ?>

Where $pinDetails->time is the datestamp in the database.

However, nothing shows, not even an error number.

EDIT:

I got it to working using this code:

$result = mysql_query("SELECT DATEDIFF(CURDATE(), '$pinDetails->time')");
$dayssince = mysql_result($result, 0);
È stato utile?

Soluzione

You're using SQL Server function syntax in MySQL. The correct syntax for MySQL's DATEDIFF is:

DATEDIFF(expr1,expr2)

For example,

SELECT DATEDIFF(CURDATE(), '2013-11-10 23:59:59')

Returns

> 10

You can replace the static date in the example with the name of the column from your table.

See a demo

Use the DBMS documentation

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top