Reputation: 2499
I used DateTime
to get the diff of two dates. Directly from the PHP documentation example:
$date1 = new DateTime('2012/03/15');
$date2 = new DateTime('2012/6/9');
$interval = $date1->diff($date2,true);
$days = $interval->format('%R%a days');
This will result in +86 days
, I wonder where can I get the reference for those %R%a
I don't know what they mean, but I just know by seeing that %R = +
while %a is number of days
.
Second, now by having the value 86
I can have at least a variable that I can use to tell that $date1
and $date2
is not within the length of 3 months (3 months is at least 90 days). I can simply use an if-else
for this, however for precision, is there another way (built-in PHP functions or library) to determine that the value I have is within the period of 3 months?
Upvotes: 0
Views: 199
Reputation: 522110
DateTime::diff
.DateInterval
, click link to its documentation.format
method.Use if ($interval->format('%m') > 3)
to test if it's over three months. Notice that this is only the months portion of the interval, e.g. "3" of "2 years, 3 months". Take the years into account as well. You should not just use days for this, since there's no constant number of days in a month. 90 days and 3 months are not the same thing.
Upvotes: 2
Reputation: 212412
http://www.php.net/manual/en/dateinterval.format.php for the docs
$months = 3;
if ($interval->format('%m') < $months) {
echo "Less than $months months";
}
Upvotes: 1
Reputation: 11301
You can find it in the documentation as well:
% Literal % %
Y Years, numeric, at least 2 digits with leading 0 01, 03
y Years, numeric 1, 3
M Months, numeric, at least 2 digits with leading 0 01, 03, 12
m Months, numeric 1, 3, 12
D Days, numeric, at least 2 digits with leading 0 01, 03, 31
d Days, numeric 1, 3, 31
a Total number of days as a result of a DateTime:diff() or (unknown) otherwise 4, 18, 8123
H Hours, numeric, at least 2 digits with leading 0 01, 03, 23
h Hours, numeric 1, 3, 23
I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59
i Minutes, numeric 1, 3, 59
S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57
s Seconds, numeric 1, 3, 57
R Sign "-" when negative, "+" when positive -, +
r Sign "-" when negative, empty when positive -,
Upvotes: 2