开发者

How to see if the date is later than 8 days?

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 dat开发者_JAVA技巧e...

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


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


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)


See the timediff function:

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


$newDate = strtotime('31-05-2010'.' -8 days');
echo date('d-F-Y',$newDate);

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


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


check TO_DAYS of mysql

TO_DAYS(DATE) - TO_DAYS('2010-03-01')) < 8


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
}


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 */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜