简体   繁体   中英

Check if a DATE and TIME has passed 3 days

I have a string with a date and time in the format 2013-07-01 19:10:05 (Ymd H:i:s) . When I output data from a database where the date and time is stored, I want to see if three days have gone since that date and time .

Example: Stored in database is 2013-07-01 00:00:00 . 3 days after that stored date and time would be 2013-07-03 00:00:00 .

If that would be true, I want to echo some text.

I tried with the following but I think I am totally out sailing.

if( strtotime('-3 days') < strtotime($row["orderdatetime"]) ) {
   echo " <img src='imgs/warning.png' ></td >";
}

Thankful for any kind of help!

Sincerely, Andreas

EDIT:

This is how my PHP script looks like.

if ($row["confirmeddatetime"] == "0000-00-00 00:00:00" ) {
        $db_datetime = new DateTime($row['orderdatetime']);
        $db_plus_three = $db_datetime->add(new DateInterval('P3D'));
        $now_datetime = new DateTime();
        if ($db_plus_three < $now_datetime) {
            echo " <img src='imgs/warning.png' ></td >";
        } else {
            echo "</td >";
        }

Can anyone of you identify if something is wrong?

I would recommend using DateTime and DateInterval classes.

$db_datetime = new DateTime($row['orderdatetime']);
$db_plus_three = $db_datetime->add(new DateInterval('P3D'));
$now_datetime = new DateTime();

if ($db_plus_three < $now_datetime) {
   // this is more than 3 days old
}

An alternate approach would be to set a flag in the DB query itself like this:

SELECT
    [YOUR CURRENT FIELDS HERE],
    (CASE WHEN NOW() > DATE_ADD(orderdatetime, INTERVAL 3 DAYS) THEN 1 ELSE 0) AS three_days_old
    [REST OF QUERY HERE]

You could then easily identify by looking at three_days_old value whether the item was more than 3 days old.

Possibly the easiest way is to get the unix time directly from the database (assuming mysql):

SELECT *, UNIX_TIMESTAMP(orderdatetime) AS ordertimestamp...

then in your comparison you just need

if( strtotime('-3 days') < $row["ordertimestamp"] ) {
   echo " <img src='imgs/warning.png' ></td >";
}

strtotime converts a time string to a timestamp which is just an integer number of seconds. Just compare to time minus 3 * 86400 (seconds in a day)

SELECT (orderdatetime <= NOW() + INTERVAL 3 DAY) AS threedays ...

you'll get a 1/0 true/false value if 3 days have gone by or not. I suggest not doing such a comparison in PHP, as you'll be forcing a mysql date -> string -> int -> date conversion chain, wasting a lot of CPU cycles when you could just do the comparison directly in mysql.

Actually your second example works for me, just be careful with the add() function, it updates the value itself. This is a similar example how i use it myself:

/**
 * Checks if the elapsed time between $startDate and now, is bigger
 * than a given period. This is useful to check an expiry-date.
 * @param DateTime $startDate The moment the time measurement begins.
 * @param DateInterval $validFor The period, the action/token may be used.
 * @return bool Returns true if the action/token expired, otherwise false.
 */
function isExpired(DateTime $startDate, DateInterval $validFor)
{
  $now = new DateTime();

  $expiryDate = clone $startDate;
  $expiryDate->add($validFor);

  return $now > $expiryDate;
}

// how to use it
$startDate = new DateTime('2013-06-16 12:36:34');
$validFor = new DateInterval('P3D'); // valid for 3 days
$isExpired = isExpired($startDate, $validFor);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM