2

I am getting a date back from a mysql query in the format YYYY-MM-DD.

I need to determine if that is more than three months in the past from the current month.

I currently have this code:

$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');

$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];

$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];

$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];

if ($diferenceInMonths > 3) {
    $log->lwrite('Need to reset password');
}

The problem with this is that, if the current month is in January, for instance, giving a month value of 01, and $resetMonth is November, giving a month value of 11, then $differenceInMonths will be -10, which won't pass the if() statement.

How do I fix this to allow for months in the previous year(s)? Or is there a better way to do this entire routine?

marky
  • 4,878
  • 17
  • 59
  • 103
  • 2
    You could just select the difference in days in your query using [DATEDIFF()](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff) – Anigel Jul 22 '13 at 16:11
  • 1
    You could do this in MySQL using (EDIT:) DATEDIFF() I believe, but if you have to do it in PHP: http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php – aaron-bond Jul 22 '13 at 16:14

3 Answers3

1

Use strtotime(), like so:

$today = time(); //todays date 
$twoMonthsLater = strtotime("+3 months", $today); //3 months later

Now, you can easily compare them and determine.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

I’d use PHP’s built-in DateTime and DateInterval classes for this.

<?php

// create a DateTime representation of your start date
// where $date is date in database
$resetDate = new DateTime($date);

// create a DateIntveral representation of 3 months
$passwordExpiry = new DateInterval('3M');

// add DateInterval to DateTime
$resetDate->add($passwordExpiry);

// compare $resetDate to today’s date
$difference = $resetDate->diff(new DateTime());
if ($difference->m > 3) {
    // date is more than three months apart
}
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • This worked as is, with one exception. The $passwordExpiry line should read: `$passwordExpiry = new DateInterval('P3M');` (You forgot the "P" in the contstruct...) – marky Jul 22 '13 at 16:42
  • D’oh, sorry about that! Glad you got it sorted though. – Martin Bean Jul 22 '13 at 21:25
-1

I would do the date comparison in your SQL expression.

Otherwise, PHP has a host of functions that allow easy manipulation of date strings: PHP: Date/Time Functions - Manual

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • 1
    What happens if OP needs to change the reset interval? Or if OP changes the database engine whose API doesn’t support `DATEDIFF()`? OP then has to re-write SQL statement rather than change a configuration value in their application. – Martin Bean Jul 22 '13 at 16:18
  • Which is why I also suggested using PHP datetime functions. Either method has its caveats. In this instance, he may already have stored procedures which handle user authentication for his application, in which case it would be trivial to add this functionality in his SQL statement(s) (provided of course that this database engine supports it). – ElGavilan Jul 22 '13 at 16:39