1

Forward:

I've scanned through the existing questions/answers on this matter. This is not a duplicitous question; I cannot find a working solution from the accepted answers.

The main questions/answers I've reviewed can be found here: How to calculate the difference between two dates using PHP?

What I need:

A calucalation of the difference between two dates expressed as years, months and days that works with PHP version: 5.2.

<?php
$current_date = date('d-M-Y');
$future_date = '2012-11-01';
?>

What I've tried:

  1. Most answers I find online don't seem to be exact in that they don't factor in leap years.
  2. This highly rated answer won't work because DateTime->diff() is php 5.3+.
  3. This accepted answer (i.e. the second block of code aimed at PHP 5.2) results in the following being parsed:

    Array ( [y] => 25 [m] => 11 [d] => 7 [h] => 3 [i] => 15 [s] => 19 [invert] => 0 [days] => 9473 ) Array ( [y] => 25 [m] => 11 [d] => 7 [h] => 3 [i] => 15 [s] => 19 [invert] => 1 [days] => 9473 )

I can't tell if I've incorrectly applied the code or it's simply a case of me not knowing how to manipulate the array.

Community
  • 1
  • 1
Dominor Novus
  • 2,248
  • 5
  • 29
  • 35
  • 4
    Don't ask what works with PHP 5.2 as of today. Ask how to change the PHP version. – hakre Oct 17 '12 at 23:02
  • Do you need textual difference or mathematical difference? If mathematical, what average values do you want to use for years and months? – Alix Axel Oct 17 '12 at 23:02
  • @AlixAxel - I may be misunderstanding you but here is an example of the desired output: `1 year, 2 months and 3 days`. – Dominor Novus Oct 17 '12 at 23:20
  • @hakre - Understood. Though I have access to cPanel/WHM, I'm presuming that this is something my host provides. I'll launch a ticket in the morning either way. – Dominor Novus Oct 17 '12 at 23:22
  • 2
    Yes, stress the hoster on this. It's a service you pay for. – hakre Oct 17 '12 at 23:30
  • @hakre - It turns out that the upgrade is something I can perform myself. I'll defer it to this weekend when I have the time to rigorously check all websites for deprecation, which I'm presuming is simply going to be a case of checking (i.e. CTRL + F) all documents for deprecated features on this list from the manual: http://php.net/manual/en/migration53.deprecated.php – Dominor Novus Oct 18 '12 at 12:48
  • 1
    Consider to have multiple PHP versions in parallel so you can switch more differentiated, e.g. when there are problems with the move to PHP 5.3. So you keep the risk low. – hakre Oct 18 '12 at 12:51

2 Answers2

1

Have you seen this comment on the manual page for DateTime::diff()?

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • No, I didn't notice that comment in the manual but thanks for bringing it to my attention. I've copied the functions to my web document and attempted to call the first functions by pasting this code right after it: http://pastebin.com/twwyET8R – Dominor Novus Oct 17 '12 at 23:39
  • Thanks for the edit Alix. I was able to output the years, months and days as separate variables thanks to your code. Does your "quick solution" account for leap years (or does it even need to in the first place)? – Dominor Novus Oct 17 '12 at 23:57
  • 1
    @DominorNovus: Leap years are just necessary for a arithmetic approach. I'm computing the textual difference (the one we use to know how old we are and when is our birthday). – Alix Axel Oct 17 '12 at 23:59
  • 1
    This solution is incorrect as months have 28, 29, 30 or 31 days in them. – Ed Heal Oct 18 '12 at 00:15
  • @DominorNovus: This is incomplete. Please unaccept it so I can delete it. – Alix Axel Oct 18 '12 at 00:48
  • Unmarked as requested. My question has been locked due to being too "localized" :-( – Dominor Novus Oct 18 '12 at 00:54
  • @DominorNovus: As for your first comment, you should do `var_dump(get_timespan_string(new DateTime($older), new DateTime($newer)));`. – Alix Axel Oct 18 '12 at 07:36
  • Thanks for your time Alix. I'm going to follow your collective advice and upgrade to PHP 5.3. – Dominor Novus Oct 18 '12 at 12:42
0

I too was working on the same thing. This might Work! First you need to create dates.

$date1=date_create("2012-03-15");
$date2=date_create("2013-03-15");

Here's the manual for that! Then to find the difference between dates and printing the desired result use this!

$dif=date_diff($date1,$date2); //Will calculate the difference between two dates.
$days = $dif->days; //Days
$years = round($age/365); //Converting days to years and rounding them up.
$months = $difference/30; // Converting to months.
echo "Days =>".$days;
echo "Years =>".$years;
echo "Months =>".$months;

Now the answer that you'll get won't be precise because there is no method in this that is calculating leap years, but if precision is not what you are looking for then this will definitely work! I Know this question was asked a long time ago, but in my defense i just saw it!

Minks
  • 55
  • 1
  • 2
  • 10