cusspvz
cusspvz

Reputation: 5291

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 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

Upvotes: 3

Views: 3625

Answers (8)

gnarf
gnarf

Reputation: 106372

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

Upvotes: 1

bPizzi
bPizzi

Reputation: 1025

With PHP >= 5.3, you can use the new shiny DateTime object (https://www.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 */
}

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

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
}

Upvotes: 0

Salil
Salil

Reputation: 47512

check TO_DAYS of mysql

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

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212452

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

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

Upvotes: 1

ULysses
ULysses

Reputation: 978

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

Upvotes: 10

Haim Evgi
Haim Evgi

Reputation: 125564

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)

Upvotes: 3

Related Questions