Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

PHP Month Calucation

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
PranjalShah
Asked:
PranjalShah
  • 4
  • 3
  • 2
  • +2
1 Solution
 
ropennerCommented:
$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
 
gyoregCommented:
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
 
Ray PaseurCommented:
Where did this date format come from?  Any reason you are not using the ISO8601 standard for the date format?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ropennerCommented:
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
 
PranjalShahAuthor Commented:
@Ray, I get the data from 3rd party so i dont have any control on what format they use.
0
 
PranjalShahAuthor Commented:
I have PHP 5.2.10
0
 
Shinesh PremrajanTechnical ManagerCommented:
$datetime1= DateTime::createFromFormat('M-Y', $input_date);
$datetime2= new DateTime();
$interval = $datetime1->diff($datetime2);

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

Hope this helps
0
 
PranjalShahAuthor Commented:
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
 
Ray PaseurCommented:
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
 
PranjalShahAuthor Commented:
Thats perfect Ray, thanks
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question, ~Ray
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now