Link to home
Start Free TrialLog in
Avatar of Brian Covington
Brian Covington

asked on

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.
Avatar of acseven
acseven
Flag of Portugal image

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

ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian Covington
Brian Covington

ASKER

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!