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'";
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'";
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'";
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.