1

I have looked at this question but time format is different

I have following date format Tue, 11 Sep 2012 17:38:09 GMT in $pubDate variable

I would like to compare $pubDate with current Date and Time to see if Tue, 11 Sep 2012 17:38:09 GMT is in last 10 mins or not

EDIT:

I have tried

//get current time
                strtotime($pubDate);
                time() - strtotime($pubDate);
                if((time()-(60*10)) < strtotime($pubDate)){
                    //if true increase badge by one
                    $badge = $badge + 1;
                }

it gives the warning: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /Users/xxxxx/Desktop/xxxx/xxxx/xxxx.php on line 26

EDIT:

I have added date_default_timezone_set('America/New_York'); line to my php and now

$inDate  = DateTime::createFromFormat( $format, $pubDate);
    $postDate = new DateTime();

    $diff = $inDate->diff( $postDate);

    // If the total number of days is > 0, or the number of hours > 0, or the number of minutes > 10, then its an invalid timestamp.
    if( $diff->format( '%a') > 0 || $diff->format( '%h') > 0 || $diff->format( '%i') > 10) {
     die( 'The timestamps differ by more than 10 minutes');
    }

works without warning, Thanks everyone

Community
  • 1
  • 1
Mord Fustang
  • 1,523
  • 4
  • 40
  • 71

4 Answers4

2

Use DateTime to do the comparison:

$format = 'D, d M Y H:i:s O';
$tz = new DateTimeZone( 'America/New_York');

// Create two date objects from the time strings
$pubDate  = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);
$postDate = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);

// Compute the difference between the two timestamps
$diff = $pubDate->diff( $postDate);

// If the total number of days is > 0, or the number of hours > 0, or the number of minutes > 10, then its an invalid timestamp.
if( $diff->format( '%a') > 0 || $diff->format( '%h') > 0 || $diff->format( '%i') > 10) {
    die( 'The timestamps differ by more than 10 minutes');
}

You can play around with it and see it working in this demo.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • +1 for using `DateTime` instead of the old `date` and `time` functions – Havelock Sep 13 '12 at 13:46
  • Florent, thanks for the tip. I typically avoid the constructor, I'm not sure why. And thanks Havelock, the DateTime class is a gem that is mostly undiscovered. – nickb Sep 13 '12 at 13:48
  • 1
    You loose the potential of DateTime comparing each element with strings. Use DateTime +1, your `if` -100 – Maks3w Sep 13 '12 at 13:49
  • @Maks3w - What element is being compared with strings? – nickb Sep 13 '12 at 13:50
  • @nickb The output of ->format is a string. You transform from string to DateTime to convert again to string and compare – Maks3w Sep 13 '12 at 13:51
  • Because DateTime is parsing the times into the sub-categories of the time. Your answer, besides not being functional, is only subtracting time off the current time, and isn't using the input string format. – nickb Sep 13 '12 at 13:54
  • It works but I am still getting the WARNING `It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /Users/` – Mord Fustang Sep 13 '12 at 14:16
  • @Mord - That is a valid timezone identifier. You should see if passing the timezone to DateTime fixes this, I'll update my answer. – nickb Sep 13 '12 at 14:19
  • You need to set the timezone in php.ini – Maks3w Sep 13 '12 at 14:28
  • and also how can I get current time automatically from system on line `$postDate = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:48:09 GMT');` – Mord Fustang Sep 13 '12 at 14:31
  • @Mord - Use `$postDate = new DateTime();`, that's the current system date. – nickb Sep 13 '12 at 14:31
  • @nickb I have already tried that it gives `Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct():` I guess i have to find a way to change php.ini settings as Maks3w said – Mord Fustang Sep 13 '12 at 14:33
  • @Mord - No, that'll work, you just have some other error message that's being suppressed or not posted. Catch the exception and print the message, then fix the problem that it says. It might be because you need a timezone: `$postDate = new DateTime( "now", $tz);`, where `$tz` is copied from above. – nickb Sep 13 '12 at 14:55
2

You can compare two DateTime objects.

$nowLessTenMinutes = new DateTime();
$nowLessTenMinutes->sub(new DateInterval('PT10M')); // Sub 10 minutes

if ($myTime >= $nowLessTenMinutes);
Maks3w
  • 6,014
  • 6
  • 37
  • 42
  • 1
    This answer is more PHP 5.3 style – Maks3w Sep 13 '12 at 13:48
  • How can this work, the parameters to the `if` are `integer >= object`. It fails with `Notice: Object of class DateTime could not be converted to int`, as expected. – nickb Sep 13 '12 at 13:53
  • $myTime is a DateTime object. I omitted how to pass from string to DateTime. I think that we shouldn't give full answers and give hints – Maks3w Sep 13 '12 at 13:55
1

Use DateTime::diff() to calculate the difference:

$input = new DateTime( 'Tue, 11 Sep 2012 17:38:09 GMT' );
$now = new DateTime();

/* calculate differences */
$diff = $input->diff( $now );

echo $diff->format( '%H:%I:%S' );
feeela
  • 29,399
  • 7
  • 59
  • 71
0

I had same issue, if you are using MAMP or similar thing it will be complicated to change php.ini try to add date_default_timezone_set('America/New_York'); on top of your php file.

Then most of the other answers on this thread should work.

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82