views:

76

answers:

8

Hi all, i have a string field (and i can't change this because the date format) on mysql database that contains a date (Like: "01-03-2010"), and i wan't to make a function to compare that date and return true if today's date is newer than 8 days, and false if the date is lower or bigger than today's date...

Example:

01-03-2010 < (08-06-2010(Today) - 8days) - return true
01-06-2010 < (08-06-2010(Today) - 8days) - return false
31-05-2010 < (08-06-2010(Today) - 8days) - return true

i know that i can convert the string "01-03-2010" to timestamp with strtotime() function on PHP, but i don'w know how to remove 8 days from today's timestamp... :s

Thanks in advance

+2  A: 

See the timediff function:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

Marc van Kempen
that will not work with mysql because the field type is a string field and not date field.
CuSS
so firs change your string to date with STR_TO_DATE() or consider to change your table fild type to date
Dobiatowski
sorry, goofed up, use the following expression to see if the difference between two dates is more than 8 days (see also the mysql date_add() function):select '2008-12-20' + interval 8 day > '2008-12-29';
Marc van Kempen
+3  A: 

to remove 8 days in php you can do :

$date_less_8 = time() - (8*24*60*60);

you can check in mysql query like :

DayDate < DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 8 DAY)
Haim Evgi
+7  A: 

strtotime(time_str) < strtotime("-8 day")

ULysses
simple and fast +1
CuSS
A: 
$newDate = strtotime('31-05-2010'.' -8 days');
echo date('d-F-Y',$newDate);

$eightdaysagoDate = strtotime('-8 days');
echo date('d-F-Y',$eightdaysagoDate);
Mark Baker
A: 

check TO_DAYS of mysql

TO_DAYS(DATE) - TO_DAYS('2010-03-01')) < 8
Salil
A: 

i tend to use these defines in my applications:

//Time Settings
if(!defined('SECOND'))  define('SECOND',  1);
if(!defined('MINUTE'))  define('MINUTE',  60  * SECOND);
if(!defined('HOUR'))      define('HOUR',    60  * MINUTE);
if(!defined('DAY'))    define('DAY',     24  * HOUR);
if(!defined('WEEK'))      define('WEEK',     7  * DAY);
if(!defined('MONTH'))     define('MONTH',   30  * DAY);
if(!defined('YEAR'))      define('YEAR',    365 * DAY);

Then

if($user_time < (time() - (DAY*8)))
{
   //Whoopsie
}
RobertPitt
A: 

With PHP >= 5.3, you can use the new shiny DateTime object (http://fr2.php.net/manual/fr/datetime.modify.php) :

$date_from_mysql = new DateTime(/*put your date from mysql here, format yyyy-mm-dd*/);

$date_today = new DateTime();

$date_8_days_ago = new DateTime()
$date_8_days_ago->modify("-8 days");

if(($date_8_days_ago <= $date_from_mysql) && ($date_from_mysql <= $date_today)) {
  /* your date from mysql is between today and 8 days ago */
} else if($date_from_mysql <= $date_8_days_ago) {
  /* your date from mysql is before 8 days ago */
} else {
  /* your date is in the future */
}
bPizzi
A: 

This could still be handled inside MySQL using the STR_TO_DATE(string, format) function:

SELECT 
  *, STR_TO_DATE(dateColumn, '%d-%m-%y') < CURDATE() - INTERVAL 8 DAY as eightdaysold 
FROM myTable
gnarf