0

I am new to working with timestamps and I am trying to come up with a way to determine if the current date is less than 48 hours away than the date stored in a Database.

I was wondering if someone can help me accomplish this?

This is what I tried, I am not sure if this is the best solution:

//future date stored in DB
$timestamp = strtotime($regularFormatDate);

if($timestamp < strtotime("+2 days")){
    //do something
}

I appreciate any advice!

Many thanks in advance!

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231

2 Answers2

1

Your example does not look bad. Actually, I'm more concerned about how you intend to handle leap years/daylight savings.

If using PHP 5.3 or above. Except in insist on using timestamp, I'd suggest (supports Daylight saving, etc):

Procedural

      $datetime1 = date_create('now');
        $datetime2 = date_create('2013-03-07');
        $interval = date_diff($datetime1, $datetime2);
        $days = $interval->format('%R%a');
        if ((int) $days >= 2) {
            echo '2 or more days';
            echo '<br />';
            echo 'Difference: ' . $days . ' days';
        } else {
            echo 'Bearly ' . $days . ' day or less';
        }

Object

        $date1 = new DateTime("2013-03-09");
        $date2 = new DateTime("now");
        $interval = $date1->diff($date2);
        $difference =  $interval->days;
        if($difference >= 2){
            echo $difference;
        }

You could try using mysql directly:

Read this: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

mysql> SELECT INTERVAL 1 DAY + '2008-12-31';
    -> '2009-01-01'

Mysql date_add() and datediff() are also good alternatives.

PHP Datetime Diff: http://www.php.net/manual/en/datetime.diff.php

Steward Godwin Jornsen
  • 1,181
  • 1
  • 7
  • 13
0

OR:
$t = time() // time of the day in Unix format
$t += 172800; // Number of seconds in 48 hours - 48 hours from now

Habib
  • 1