Learn when you want, where you want with convenient online training courses. Sign up now!
[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More
Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
$oldDate = DateTime::createFromFormat('M-y', $olddatestring, '0:0');
$diff = $oldDate->diff(new DateTime());
$diff_days = $diff->y * 12 + $diff->m;
// check diff docs at http://hu.php.net/manual/en/datetime.diff.php for further info...
Open in new window
<?php // RAY_temp_months.php
echo "<pre>" . PHP_EOL;
// A FUNCTION TO COUNT MONTHS
// MUNG THE DATA INTO A USABLE STRING AND GET TIMESTAMP
$old = str_replace('-', ' 1, ', $old);
$old_ts = strtotime($old);
if ($old_ts === FALSE) return FALSE;
// FIRST DAY OF THIS MONTH
$new = date('M') . ' 1, ' . date('Y');
$new_ts = strtotime($new);
// MONTH COUNTER
$months = 0;
while ($new_ts > $old_ts)
$new_ts = strtotime(date('Y-m-d', $new_ts) . ' - 1 month');
// TEST DATA FORMAT M-y AS SHOWN ON EE
// SEE http://us.php.net/manual/en/function.date.php
echo PHP_EOL . months_ago('Apr-09');
// SOME OTHER SAMPLES
echo PHP_EOL . months_ago('Aug-10');
echo PHP_EOL . months_ago('Jul-10');
echo PHP_EOL . months_ago('Jun-10');
echo PHP_EOL . months_ago('DEC-10');
echo PHP_EOL . months_ago('Jan-10');
echo PHP_EOL . months_ago('Sep-01');
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.