Alk
Alk

Reputation: 5557

problems in comparing two dates in php, minutes always 0

I'm gonna need to set up same date comparisons and I'm practicing first with this php code, however no matter how I alter the date I end up getting the hours and seconds but the minutes is always 0. Btw, ideally I would like to be able to write the date in the european format dd/mm/yy hh:mm however this didn't work so I adjusted it to the US format. Anyway thanks for the help.

date_default_timezone_set('Europe/London');
$date= new DateTime('2015-06-21 18:32:00');
$now = new DateTime();
$interval = $date->diff($now);
echo "difference " . $interval->d . " days, " . $interval->h." hours, ".$interval->m." minutes";

Upvotes: 0

Views: 46

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

You made a simple mistake with the property used to represent the intervals minutes, its i and not m. m is in fact the Months property.

This is also how you would set the date using the European format

$date = DateTime::createFromFormat('d/m/Y H:i:s', '21/06/2015 18:32:00');
$now = new DateTime();
$interval = $date->diff($now);
echo "difference " . $interval->d . " days, " . $interval->h." hours, ".$interval->i." minutes";

If you do a print_r($interval) you will get to see all the properties like this

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 6
    [i] => 4
    [s] => 15
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 0
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

Upvotes: 2

user2182349
user2182349

Reputation: 9782

Use $interval->i for minutes, $interval->m is for months.

Upvotes: 2

Related Questions