PHP Convert Timestamp to Months and Days

How can I convert the difference between timestamps between two dates

$diff = abs(strtotime($date2) - strtotime($date1)); 

up to the number of months and days as the desired output:

 $res = 4.05 //-> 4 months and 5 days 
+4
source share
10 answers

Something like this might work. My math may be a little 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)); 
+7
source

You cannot approach it in the same way as the length of the month, unless you want to make an assumption based on the average length of the month. If you need 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

+4
source

For PHP 5.3 you can use http://de.php.net/manual/en/datetime.diff.php

For lower versions, you can use something like: (Simple solution, not faster or better)

 $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: unverified

+3
source

Timestamp is actually the number of seconds since Unix Epoch, i.e. January 1, 1970. So the difference in timestamps is seconds. Therefore, to convert seconds to several days, you just need to divide it by the number of seconds per day, i.e. $ Timestamp / (24x60x60). Same for the month $ Timestamp / (24x60x60x30)

+2
source

The biggest problem you will face is determining the months. Will you use 4 weeks = 1 month, 30 days = 1 month, etc. I will be tempted to either leave the figure in the form of several days, or, at most, convert it to weeks and days.

+1
source

I don't think this is available in php. Thus, you will have to use integer division (or normal division) and modulo operator.

For example (the example uses a clock instead of ms, but the same trick is repeated):

 $totalhours = 27; $days = round($totalhours / 24); $hours = $totalhours % 24; 

This will give $ days = 1 and $ hours = 3

As indicated in other answers. Searching for months is more difficult because the number of days per month is not fixed.

+1
source

You might want to take a look at DateTime.diff , as it can express the differences that you expect from a person when comparing months (for example, from 01/01/2010 to 01/01/2010 - the difference is (exactly) three months).

+1
source

You can calculate the difference in dates per day using this function

 function diff_date_day($day , $month , $year , $day2 , $month2 , $year2){ $timestamp = mktime(0, 0, 0, $month, $day, $year, 0); $timestamp2 = mktime(0, 0, 0, $month2, $month2, $year2); $diff = floor(($timestamp - $timestamp2) / (3600 * 24)); return $diff; } 

Suppose the average month is 30 days and calculates the number of months. This may be enough for some needs (showing the age of the blog comments, etc.) and completely unsuitable for others.

+1
source

Found a solution using MySQL to calculate the months:

SELECT TIMESTAMPDIFF (MONTH, '{$ start_time_str}', '{$ end_time_str}') AS m

Thanks to everyone.

0
source

I know this is already old, but I came across this when I was looking for something to calculate the time difference in the β€œ3 years and 6 months” format from a given amount in the timestamp format. I wrote the following, which may seem useful.

 // Determine age $time_start = 'YOUR DOB'; $time_end = strtotime('today'); $years = 0; $months = 0; while ($time_start < $time_end){ $tmp_time = strtotime('+ 1 year', $time_start); if ($tmp_time < $time_end){ $years++; $time_start = $tmp_time; } else { $tmp_time = strtotime('+ 1 month', $time_start); if ($tmp_time < $time_end){ $months++; $time_start = $tmp_time; } else { $time_start = $time_end; } } } $age = $years.' years &amp; '.$months.' months'; 
0
source

Source: https://habr.com/ru/post/1300167/


All Articles