[Webinar] Learn how to a build a cloud-first strategyRegister Now


Help with PHP date function

Posted on 2014-08-18
Medium Priority
Last Modified: 2014-08-18
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:

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.
Question by:Brian Covington
  • 2
LVL 31

Expert Comment

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

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

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:


$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

LVL 31

Accepted Solution

Frosty555 earned 2000 total points
ID: 40269402
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


Author Closing Comment

by:Brian Covington
ID: 40269413
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!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question