Solved

PHP Month Calucation

Posted on 2010-08-18
11
326 Views
Last Modified: 2013-12-13
I have data in date format mmm-yy like Apr-09. What I want to do is find out how old the date is from current date. I want to find out the number of months from current month.
0
Comment
Question by:PranjalShah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:ropenner
ID: 33469899
$input_date = "Apr-09";
$date = DateTime::createFromFormat('M-Y', $input_date);
$date_formatted = $date->format('Y-m-d');
$after = new DateTime(date("Y-m-d"));
$before = new DateTime($date_formatted);
$interval = $after->diff($before);
echo $interval->format('%y years + %m months + %d days');
0
 
LVL 5

Expert Comment

by:gyoreg
ID: 33469900
If you have >=5.3, then you can do this, assuming $olddatestring is filled with a date in the specified format:
$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

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33469906
Where did this date format come from?  Any reason you are not using the ISO8601 standard for the date format?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ropenner
ID: 33469918
Had an error above ... Y was supposed to be 'y' since your year is only two digits.

$input_date = "Apr-09";
$date = DateTime::createFromFormat('M-y', $input_date);
$date_formatted = $date->format('Y-m-d');
$after = new DateTime(date("Y-m-d"));
$before = new DateTime($date_formatted);
$interval = $after->diff($before);
echo $interval->format('%y years + %m months + %d days');


# output is:   1 years + 4 months + 0 days
0
 
LVL 8

Author Comment

by:PranjalShah
ID: 33469978
@Ray, I get the data from 3rd party so i dont have any control on what format they use.
0
 
LVL 8

Author Comment

by:PranjalShah
ID: 33470017
I have PHP 5.2.10
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 33472183
$datetime1= DateTime::createFromFormat('M-Y', $input_date);
$datetime2= new DateTime();
$interval = $datetime1->diff($datetime2);

echo $interval->format('%R%d days');

Hope this helps
0
 
LVL 8

Author Comment

by:PranjalShah
ID: 33477711
I think the function createFromFormat is not supported in my version of PHP 5.2.10 ...Let me know if there is turn around
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33483710
Here is a function that will compute it correctly for you.  Months are a little tricky because (unlike weeks, days, hours, minutes) they do not have the same number of seconds.  But the PHP functions strtotime() and date() play together well enough to enable us to get the right answers by asking something like "Today - 1 month"

best regards, ~Ray
<?php // RAY_temp_months.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// A FUNCTION TO COUNT MONTHS
function months_ago($old)
{
    // 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');
        $months++;
    }

    return $months;
}

// 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');

Open in new window

0
 
LVL 8

Author Closing Comment

by:PranjalShah
ID: 33489059
Thats perfect Ray, thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33491409
Thanks for the points - it's a great question, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I fix this error with quotation marks? 4 49
if statement malfunction 5 44
Change background images after 5 seconds. 12 46
Developers / Staff Setup 10 41
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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