0

I need a little help with this code:

$key['time_stamp'] = '2016-09-02 16:56:25';
$datetime1 = strtotime(date($key['time_stamp']));
$datetime2 = strtotime(date('Y-m-d H:m:s'));
$interval = $datetime2 - $datetime1;
$val = 45 * 60 * 1000;

if ($interval > $val) { }

My purpose is to check if the $interval between two dates is greater than 45 mins.

$key['time_stamp'] is 2016-09-02 16:56:25 and datetime2 is the current date time. Why the if condition is never true?!

Did I miss something stupid or what?!

COil
  • 7,201
  • 2
  • 50
  • 98
Clorge32
  • 341
  • 1
  • 3
  • 7
  • Unless I'm blind, you're missing something totally important: You need to tell people which programming language you're talking about! I'm guessing this is about perl, and added a tag to that effect. If I was wrong, please clarify or correct or something! – Carl Smotricz Sep 05 '16 at 10:01
  • right, I'm using php :) – Clorge32 Sep 05 '16 at 10:04
  • have you tried this: http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php ? – caramba Sep 05 '16 at 10:06
  • yes I already tried whit that solution – Clorge32 Sep 05 '16 at 10:22
  • I've broken down and bungled my way through writing what I think should be a workable solution. Check my edited answer. – Carl Smotricz Sep 05 '16 at 10:26

5 Answers5

2

If $key['time_stamp'] is '2016-09-02 16:56:25', shouldn't it be

$datetime1  = strtotime ( $key['time_stamp'] );

Instead of

$datetime1  = strtotime ( date ($key['time_stamp']) );

?

Mateusz Szymik
  • 184
  • 1
  • 5
1

This line:

$val = 45*60*1000;

gives 45 minutes in millisecond, I guess you want:

$val = 45*60;
Toto
  • 89,455
  • 62
  • 89
  • 125
0

You made two mistakes:

  1. the minutes in the $format parameter of the date function have to be expressed as i (and not m, as you did).
  2. $interval is measured in seconds (so you don't have to multiply by 1000, as if they were milliseconds).

So your code should be written as follows:

$key['time_stamp'] = '2016-09-02 16:56:25';
$datetime1 = strtotime(date($key['time_stamp']));
$datetime2 = strtotime(date('Y-m-d H:i:s'));
$interval = $datetime2 - $datetime1;
$val = 45 * 60;

if ($interval > $val) { }
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
0

I find working with DateTime() objects easier to reason about, and they are transparently comparable in an if statement.

I'd like to suggest an alternative approach:

<?php

$key['time_stamp'] = '2016-09-02 16:56:25';

$then = new DateTime($key['time_stamp']);
$interval = new DateTime('-45 minutes');

if ($then < $interval) {
    echo 'interval passed';
} else {
    echo 'interval not passed';
}

This yields:

interval passed

Hope this helps :)

One caveat that's not applicable in your case but it's worth mentioning: If you create a DateTime() object with a UNIX timestamp using DateTime::createFromFormat() be sure to check that both DateTime() objects have the same timezone.

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
0

Here is the solution of your query.

$key['time_stamp'] = "2016-09-02 16:56:25";
   $datetime1 = strtotime ( date ($key['time_stamp']) );
   $datetime2  = strtotime ( date('Y-m-d H:i:s'));

   $interval   = round(($datetime2 - $datetime1)/ 60); //interval in minutes

   $val = 45; //45 minutes
   if ($interval > $val) {
     echo "Success";
   } 
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57