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";
Alk
  • 5,215
  • 8
  • 47
  • 116
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Alessandro Da Rugna Jun 21 '15 at 12:27
  • No, I already checked all of those to come up with this code, it doesn't solve the problem tho, the answer below did. – Alk Jun 21 '15 at 12:29

2 Answers2

2

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

user2182349
  • 9,569
  • 3
  • 29
  • 41
2

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
)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Can this be incorporated into a SQL query, I want to load all items where the date field is less than the current date.. would it be something like `SELECT * FROM table WHERE datefield < NOW()`? – Alk Jun 21 '15 at 12:34
  • Yes but thats really another question. You can also use `WHERE datefield BETWEEN '2015-01-01 00:00:01' AND '2015-02-01 00:00:01'` type syntax – RiggsFolly Jun 21 '15 at 12:36
  • I know but I don't want to wait 90 minutes to ask another question, so I'd really appreciate if you could be able to help me here. I don't really understand how that would work tho, I don't need it to be in any interval as long as it's less than the current time – Alk Jun 21 '15 at 12:38
  • The syntax is exactly as you used in your first comment – RiggsFolly Jun 21 '15 at 12:45