How can I convert a timestamp difference between 2 dates ( $diff = abs(strtotime($date2) - strtotime($date1)); ) to months and days quantity (ouput: $res = 4.05 -> 4 months and 5 days)?
Thanks.
How can I convert a timestamp difference between 2 dates ( $diff = abs(strtotime($date2) - strtotime($date1)); ) to months and days quantity (ouput: $res = 4.05 -> 4 months and 5 days)?
Thanks.
The biggest problem you are going to face here is defining months. Are you going to use 4 weeks = 1 month, 30 days = 1 month, etc... I would be tempted to either leave the figure as a number of days, or at most, convert to weeks and days
Timestamp is actually the number of seconds since Unix Epoch i.e. 1 Jan 1970. So differences in timestamps is actually seconds. So to convert seconds into days you just need to divide it by number of seconds in a day i.e. $Timestamp/(24x60x60). Same for the month $Timestamp/(24x60x60x30)
I don't think this is available in php. So you will have to use integer division (or normal division) and modulo operator.
For instance (example uses hours instead of ms but it is the same trick repeated):
$totalhours = 27;
$days = round($totalhours / 24);
$hours = $totalhours % 24;
This will give $days = 1 and $hours = 3
As stated in other replies. Finding months is harder because the amount of days per month isn't fixed.
Something like this would possibly work. My maths may be a bit off.
$diff = abs(strtotime($date2) - strtotime($date1));
define('DAY',60*60*24, true);
define('MONTH',DAY*30, true);
define('YEAR',DAY*365, true);
$years = floor($diff / (YEAR));
$months = floor(($diff - $years * YEAR) / (MONTH));
$days = floor(($diff - $years * YEAR - $months*MONTH ) / (DAY));
You can't approach it like that as month length varies - unless you want to make an assumption based on average month length. If you want the correct difference you need to use the actual dates then you can use php date diff - http://www.php.net/manual/en/datetime.diff.php
You might want to take a look at DateTime.diff, as it is able to express differences you would expect from a human when comparing months (like 01.01.2010 to 01.03.2010 is a difference of (exactly) three months).
For PHP 5.3 you might want to use
http://de.php.net/manual/en/datetime.diff.php
For lower versions you might want to use something like: (Simple solution, not fastest or best)
$days = 0;
$months = 0;
$month = date("n", $time1);
while ($time1 < $time2)
{
++$days;
if (date("n", $time1) != $month)
{
$month = date("n", $time1);
$days = 0;
$months++;
}
$time1 = mktime(0, 0, 0, date("n", $time1), date("j", $time1) + 1, date("Y"));
}
//Disclaimer: untested
You can calculate date difference in day, with a function like this
function diff_date_day($jour , $mois , $an , $jour2 , $mois2 , $an2){
$timestamp = mktime(0, 0, 0, $mois, $jour, $an,0);
$timestamp2 = mktime(0, 0, 0, $mois2, $jour2, $an2,0);
$diff = floor(($timestamp - $timestamp2) / (3600 * 24));
return $diff;
}
The make an assumption an average month is 30 days and calculate the number of months. It can be enough for some needs (displaying blog comment age, etc.) and totally unadapted for others.
Found a solution using MySQL to calculate the months:
SELECT TIMESTAMPDIFF(MONTH,'{$start_time_str}','{$end_time_str}') AS m
Thanks to all involved.