3

Say I have the number 234 edit (in days). How can I find how many months ,weeks and days? Is there a function or I have to calculate them myself? My main problem is the months cause they have no fixed number of days (30,31,28).

Actually I am writing an app where the time per charge is entered in days. But in the view I don't want to display "charge per 234 days" but "charge per x months y week n days"

Thanks in advance

chchrist
  • 18,854
  • 11
  • 48
  • 82
  • What does the number (234) *mean*? Hours? Weeks? Days? Years? Seconds? From what base date? From the start of the year? From today? From my birthday? From your birthday? Please explain what this number *means*. – S.Lott Nov 30 '11 at 11:05
  • You might want to explain that number relative to time. i.e. Is it 234 days from today, or 234 days ago. etc. Simply stating 234 is very ambiguous and will lead to an incorrect result. – Russell Dias Nov 30 '11 at 11:05
  • sorry edited. There is no starting day. There are pricing plans that say "we are going to charge you every x days" and I want to change the days to a more friendly text output. – chchrist Nov 30 '11 at 11:07
  • 1
    You say 234 is your number of days. But is that days from the start of the year? Or is it days from the UNIX epoch (Jan 1, 1970)? Or is it days from some arbitrary date? If it's from Jan 1, then Jan 1 of what year? – GordonM Nov 30 '11 at 11:07
  • @GordonM It is from the date the client bought the service but I display this text before the client buys it. – chchrist Nov 30 '11 at 11:08

2 Answers2

6

EDIT : Added number of weeks also

<?PHP
$date1 = new DateTime();
$date2 = new DateTime();
$date2->modify('+241 day');

$interval = $date1->diff($date2);

$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');

$weeks = (int) ($days / 7);
$days = $days % 7;

echo "$years years $months months $weeks weeks $days days";
?>

EDIT: Doing without DateTime below

refer: How to calculate the difference between two dates using PHP?

$date1 = time();
$date2 = strtotime("+237 day");

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

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$weeks = (int) ($days / 7);
$days = $days % 7;

printf("%d years, %d months,%d weeks, %d days\n", $years, $months, $weeks, $days);
Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
1

Finding weeks is easy enough, just divide the number of days by 7.

Finding the number of months is a lot more tricky, as the result will depend on the starting date. The number of days in a month differ from month to month, and year to year in the case of February. Without knowing a start date all you could do is approximate by dividing by the average month length (30.4375). If you want to know the actual number of months, you can compute the end date from the known start date and get the number of months difference between them.

The PHP DateTime class should provide the functionality you need to do this.

http://uk.php.net/manual/en/class.datetime.php

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • To get the remaining days? For exampe 23 days are 3 weeks + 2 days. Should I use the modulo? 23%7? – chchrist Nov 30 '11 at 11:23
  • If you need weeks and days, then yes. If you get a remainder from the weeks division then doing a mod will get the number of days left over. You really are better off using DateTime if you can though. – GordonM Nov 30 '11 at 11:25
  • Then is better using the Datetime to find how many months or weeks, if the days are not that many in order to fill a month, using as a starting date the date the user is seeing the form? – chchrist Nov 30 '11 at 11:29