How to find out if the date was more than 8 days?

I have a string field (and I canโ€™t change this because the date format is) in the mysql database that contains the date (like: "01-03-2010"), and I wonโ€™t make a function to compare this date and return true if today's date is newer than 8 days, and false if the date is less or more 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 using a function strtotime()in PHP, but I don't know how to remove 8 days from today timestamp ...: s

Thanks in advance

+3
source share
8 answers

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

+10
source

8 php, :

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

mysql, :

DayDate < DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 8 DAY)
+3
$newDate = strtotime('31-05-2010'.' -8 days');
echo date('d-F-Y',$newDate);

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

This can still be handled internally in MySQL using the function STR_TO_DATE(string, format):

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

check TO_DAYS mysql

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

I try to use these definitions 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
}
0
source

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
source

Source: https://habr.com/ru/post/1753686/


All Articles