View Single Post
Ambitious contributor
11Jun2012,03:34  
pein87's Avatar
Your query already selects none duplicates because your using the DISTINCT keyword. You will not get duplicates using that keyword nor would you if you used UNIQUE. you could just get the last item and the date of today and use php to check the date difference.

get last item using id column

Code: SQL
SELECT date FROM table_name ORDER BY id DESC LIMIT 1

or

Code: SQL
SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1;

the last method requires a proper date data type like DATETIME or TIMESTAMP and does not work on any VARCHAR types.

Your array would then have three values year, month, and day which you can use php's built in date function to get these for the current day and subtract the values to see the time elapsed.

PHP Code:
<?php
include("config.php");

$Q mysql_query("SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1");

$tDate = array(date("m"),date("d"),date("Y"));

$R mysql_fetch_array($Q);

if(
$R['lDay'] == $tDate[1] && $R['lMonth'] == $tDate[0] && $R['lYear'] == $tDate[2])
{

// some code here that says everything was done today

}
else
{

// subtract the values using $tDate as the big number ie $tDate[0] - $R['Month']

}

?>
That isn't the best example of course but you could compare which one is greater only if the years are different and calculate how many days, months, and years have passed since the last insert.

if you

PHP Code:
print_f($tDate); 
it shows

Code:
Array ( [0] => 06 [1] => 10 [2] => 2012 )
you can also get todays date using this SQL

Code: SQL
SELECT MONTH(NOW()) AS thisMonth, DAY(NOW()) AS thisDay, YEAR(NOW()) AS thisYEAR

just giving you options to get todays date with code and compare your last entries date.

Last edited by pein87; 11Jun2012 at 04:02..
shabbir like this