0

If I have

$time_interval = date_diff(date1, date2)

How can I do this in PHP?

If ($time_interval >= ('2 months and 15 days'))
    echo "time is '2 months and 15 days' or more"
else
    echo "time is less than '2 months and 15 days'"

I tried

if ($time_interval->m <= 2 and $time_interval->d < 15)

But this will return FALSE for 1 month and 20 days which is obviously wrong

Is there something like..?

$time_lapse = create_my_own_time_lapse(2months & 15 days)

Then it would be very neat to compare both

If ($time_interval >= $time_lapse)

SOLUTION

date_diff retuns a DateInterval object. I found the way to create my own DateInterval for '2 months and 15 days'. This is my updated code:

Visit PHP DateInterval manual for details

$today = new DateTime(date('Y-m-d'));
$another_day = new DateTime("2019-05-10");

$time_diff = date_diff($today, $another_day);

// 'P2M15D' is the interval_spec for '2 months and 15 days'
$time_interval = new DateInterval('P2M15D');

// Let's see our objects
print_r($time_diff);
print_r($timeInterval);

if($time_diff >= $time_interval)
    echo "<br/>time is '2 months and 15 days' or more";
else
    echo "<br/>time is less than '2 months and 15 days'";
  • Have a look at [this](https://stackoverflow.com/a/3923228/1213708) answer, which means you can `echo $time_interval->days;`, BUT if you want 2 months - which depends how many days in each month this may not be as simple as that. – Nigel Ren Oct 08 '19 at 07:00

2 Answers2

2

your code is almost correct. Just remove the and and add strtotime()

from:

if ($time_interval >= ('2 months and 15 days'))
    echo "time is '2 months and 15 days' or more";
else
    echo "time is less than '2 months and 15 days'";

to:

if ($time_interval->getTimestamp()) >= strtotime('2 months 15 days'))
    echo "time is '2 months and 15 days' or more";
else
    echo "time is less than '2 months and 15 days'";
Davit Huroyan
  • 302
  • 4
  • 16
Shiz
  • 695
  • 10
  • 27
  • `strtotime($time_interval)` returns error `strtotime() expects parameter 1 to be string, object given` because `$time_interval` is a **DateInterval** object – Marco Sánchez Oct 08 '19 at 07:28
  • My bad, can you replace the `strtotime($time_interval)` to `$time_interval->getTimestamp()` – Shiz Oct 08 '19 at 08:03
  • I think it is much better to work with `DateInterval` as I explain at the end of the question in the solution I found because `DateInterval` takes in count months with 30 or 31 days also february with 28/29 days. Anyways your answer works fine. ThankYou – Marco Sánchez Oct 08 '19 at 08:13
  • TBH, not that much knowledge about classes like DateInterval, but thanks for accepting the answer anyway. – Shiz Oct 08 '19 at 08:26
1

Easy way of doing this is converting your time to seconds and than compare these seconds with amount of seconds equal to 2 months and 15 days.

$timeInterval = strtotime('2009-12-01') - strtotime('2009-10-01');
$overSeconds = 60 * 60 * 24 * 75; // 60 seconds * 60 minutes * 24 hours * 75 days

if($timeInterval >= $overSeconds)
    echo "time is '2 months and 15 days' or more";
else
    echo "time is less than '2 months and 15 days'";
MatejG
  • 1,393
  • 1
  • 17
  • 26
  • `if($timeInterval >= $overSeconds)` returns error: `Object of class DateInterval could not be converted to int` – Marco Sánchez Oct 08 '19 at 07:02
  • Just replace dates with your date. As easy as that. Replace 2009-12-01 and 2009-10-01 with dates you want to compare. You are putting objects there instead of actual string with dates. – MatejG Oct 08 '19 at 08:01
  • I know, but in my real project code `$timeInterval` comes from `date_diff`I need to work with **DateTime Object** – Marco Sánchez Oct 08 '19 at 08:04