简体   繁体   中英

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

使用$ interval-> i分钟,$ interval-> m几个月。

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
)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM