Help with PHP date function

I am new to PHP having come over from .net and asp.  I am having difficulty creating a function that will take an "expiration date" and determine, based on that date what the "New Expiration Date" will be.  Dates in PHP are just strange.  Here is what I have so far:

<?php
function NewExpirationDate($ExpirationDate) {

$ExpirationDate = date_format($ExpirationDate, date('Y-m-d'));

if ( $ExpirationDate  > date('Y-m-d')){
      $NewExpirationDate = date_add($ExpirationDate, date_interval_create_from_date_string("1 year"));
} elseif (date_add($ExpirationDate, date_interval_create_from_date_string("90 days")) <  date) {
      $NewExpirationDate = date_add(date('Y-m-d'), date_interval_create_from_date_string("1 year"));
} else {
      $NewExpirationDate = date_add($ExpirationDate, date_interval_create_from_date_string("1 year"));
}

echo date_format($ExpirationDate,'Y-m-d');
}
?>




I am using PHP 5.4
$ExpirationDate is coming from the database and is formatted as "2014-07-01 00:00:00"


If the $ExpirationDate is later than the date today, then the $NewExpirationDate should be a year after the $ExpirationDate

If the $ExpirationDate is more than 90 days in the past, then the $NewExpirationDate should be one year from today

If the $ExpirationDate is within the last 90 days, then the $NewExpirationDate should be 1 Year after $ExpirationDate

I think I am close above. I would appreciate any help. There is not a specific error as I know that I am not properly converting dates to the proper formats for comparison.  This simple task seems so much more straightforward in .net.
Brian CovingtonAsked:
Who is Participating?
I wear a lot of hats...

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

Frosty555Commented:
Based on the logic you've described it sounds like it boils down to:

If ExpirationDate > (Today - 90 days) Then
    NewExpirationDate = ExpirationDate + 1 year
Else
    NewExpirationDate = Today + 1 year
End

PHP has a fairly rich set of functions using the DateTime class, but the strtotime() function is very simple and almost magic - it converts a wide range of string-based dates into a UNIX timestamp, which is simply the number of seconds since the epoch. Expressing the date in that format makes it really easy to compare dates or to modify a date by an amount that can be expressed in seconds. Combine this with PHP's flexible notation for expressing relative dates it makes it very easy to do what you're looking for. See here:

http://php.net/manual/en/datetime.formats.relative.php
http://ca1.php.net/manual/en/function.strtotime.php

$ExpirationDate = strtotime("2014-04-20 00:00:00");

if( $ExpirationDate > strtotime("-90 DAYS") ) {
    $NewExpirationDate = strtotime("+1 YEAR", $ExpirationDate);
    echo "expiration date is after 90 days ago<br/>";
} else {
    $NewExpirationDate = strtotime("+1 YEAR");
    echo "expiration date is before or exactly 90 days ago<br/>";
}

echo "Expiration Date: " . date("F j, Y, g:i a", $ExpirationDate) . "<br/>";
echo "New Expiration Date: " . date("F j, Y, g:i a", $NewExpirationDate) . "<br/>";

Open in new window

0
Frosty555Commented:
Keep in mind that all of this code is taking the time into account as well.  If you don't want that, you can strip the timestamp and assume midnight by using the "today" relative notation.

$ExpirationDate = strtotime("2014-04-20 00:00:00 today");

if( $ExpirationDate > strtotime("today -90 DAYS") ) {
    $NewExpirationDate = strtotime("+1 YEAR", $ExpirationDate);
    echo "expiration date is after 90 days ago<br/>";
} else {
    $NewExpirationDate = strtotime("today+1 YEAR");
    echo "expiration date is before or exactly 90 days ago<br/>";
}

echo "Expiration Date: " . date("F j, Y, g:i a", $ExpirationDate) . "<br/>";
echo "New Expiration Date: " . date("F j, Y, g:i a", $NewExpirationDate) . "<br/>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CovingtonAuthor Commented:
Unbelievably clear explanation. I was on informaiton overload trying to Google different approaches to this simple problem,  I really appreciate your time explaining the strtotime vs the DateTime functions.  Makes sense now. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.