count months

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eskimex1
Forum Newbie
Posts: 3
Joined: Mon Aug 25, 2008 6:59 am

count months

Post by eskimex1 »

I am writing for my booking website. I want people to be able to choose dates and then get sent to paypal to pay the correct amount depending on the month that the days chosen are in.

So for 2008-31-1 till 2008-2-2 they would be charged x for the August 31, and y for the other days in September.

So far I am using a function to output all months between the two dates. For the example used, I would get 1,2,2.
Then I tried to use array_count_values to find out how many times each month showed up.
BUT, it is not working the way it should... Here is that part of the code so far:

Code: Select all

 
/////// date duer
 
function getAllDays($start, $end, $aslist = true) {
$start = strtotime($start);
$end = strtotime($end);
 
$whichway = ($start < $end) ? "tomorrow" : "yesterday";
 
$curday = $start;
 
$days = array();
$days[] = date("m", $curday);
 
while ($curday != $end) {
$curday = strtotime($whichway, $curday);
$days[] = date("m", $curday);
}
 
if ($aslist === false) return $days;
$daylist = "";
foreach ($days as $day) {
$daylist .= $day.", ";
}
 
$daylist = substr($daylist, 0, -2);
 
return $daylist;
}
 
print_r(array_count_values(array(getAllDays("2005-01-01", "2005-01-24"))));
 
 
The next step would be to use the information outputted to determine that we are charging x for the day in August and y for the day in September. But I am sooooooo stuck.

I hope I am not asking for too much here. I just need a direction to go in or maybe I am doing something stupid?? ...

Please Help! :D
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

Re: count months

Post by eskio »

Hi,
In my opinion instead of using strtotime (convert to time), why don't convert it to date? it better to use getdate(), date() and mktime(). And it is also easier to compare date in timestamp (int).
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Re: count months

Post by DaiWelsh »

I would actually rewrite the logic substantially but I don't have time for that right now, so will try to make minimal changes; instead of putting the month for each day in a comma seperated list (which is not much use in later PHP) why not use an array to store the count for each month? e.g.

Code: Select all

 
$arrDaysPerMonth = array();
while ($curday != $end) {
  $curday = strtotime($whichway, $curday);
  $arrDaysPerMonth[date("m", $curday)] ++ ;
}
 
this will give you an array with the keys being the months and the values being the numbe rof days in that month, e.g. for your example

[1]=>1
[2]=>2

or for a booking from 15/3 to 7/5

[3]->16
[4]->30
[5]->7

which seems like more what you want? Inidentally have you considered year boundaries - i.e. do you care if the month is jan 2008 or jan 2009?

HTH,

Dai
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: count months

Post by starram »

Hi,

I guess this is what you need.

Code: Select all

<?php
################## Function To get days between two dates ####################
function getdays($day1,$day2)
{
  return round((strtotime($day2)-strtotime($day1))/(24*60*60),0);
}
 
$begin = date("Y/m/d"); // we set today as an example
$end = "2008/12/10";
getdays($begin,$end); // Calling the Function to count number of days between two dates.
 
################## Function To get months between two dates ####################
function getmonths($startdate,$enddate,$separator)
{
    $startdate=explode($separator,$startdate);
    $enddate=explode($separator,$enddate);
    $year=$enddate[0]-$startdate[0];
    if($enddate[2]>$startdate[2])
    {
        $month=$enddate[2]-$startdate[2];
        $year=$year*12;
        return $month=$month+$year;
    }
    else
    {
        $month=$enddate[2]-$startdate[2];
        if($year>0)
        {
            return $month=($year*12)+$month;
        }
        else
        {
            return $month=12+$month;
        }
    }
}
getmonths($begin,$end,"/"); // Calling the Function to count number of days between two dates.
?>
ahowell
Forum Newbie
Posts: 17
Joined: Mon Sep 01, 2008 9:18 pm

Re: count months

Post by ahowell »

You'll need to makes sure you cover if the start/end dates span more than 1 year. Here's a little quick snippet:
* Will only work in PHP 5.0 -> later, due to the use of date_create();

Code: Select all

function getElapsedDays($startDate, $endDate, $group=true)
{
    $startDate = date_create($startDate);
    $endDate   = date_create($endDate);
    $output    = array();
    
    if ($endDate->format('Y') > $startDate->format('Y')) {
        for ($year = $startDate->format('Y'); $year <= $endDate->format('Y'); $year++) {
            if ($year == $startDate->format('Y')) {
                $output = array_merge($output, getElapsedDays($startDate->format('m/d/Y'), date('m/d/Y', mktime(1, 0, 0, 12, 31, $startDate->format('Y')))));
            } else if ($year == $endDate->format('Y')) {
                $output = array_merge($output, getElapsedDays(date('m/d/Y', mktime(1, 0, 0, 1, 1, $year)), $endDate->format('m/d/Y')));
            } else {
                $output = array_merge($output, getElapsedDays(date('m/d/Y', mktime(1, 0, 0, 1, 1, $year)), date('m/d/Y', mktime(1, 0, 0, 12, 31, $year))));
            }
        }
    } else {
        for ($month = $startDate->format('n'); $month <= $endDate->format('n'); $month++) {
            if ($month == $startDate->format('n')) {
                $days = (date('t', $startDate->format('n')) - $startDate->format('j'));
            } else if ($month == $endDate->format('n')) {
                $days = $endDate->format('j');
            } else {
                $days = date('t', mktime(1, 0, 0, $month));
            }
            
            $output["{$month}/{$startDate->format('Y')}"] = $days;
        }
    }
    
    return ($group == true) ? $output : array_sum($output);
}
 
2/5/2008 - 7/16/2012
Grouped by month/year

Code: Select all

getElapsedDays('2/5/2008', '7/16/2012');
 
// returns:
// Array
// (
//   [2/2008] => 26
//    [3/2008] => 31
//    [4/2008] => 30
//    [5/2008] => 31
//    [6/2008] => 30
//    [7/2008] => 31
//    [8/2008] => 31
//    [9/2008] => 30
//    [10/2008] => 31
//    [11/2008] => 30
//    [12/2008] => 31
//    [1/2009] => 30
//    [2/2009] => 29
//    [3/2009] => 31
//    [4/2009] => 30
//    [5/2009] => 31
//    [6/2009] => 30
//    [7/2009] => 31
//    [8/2009] => 31
//    [9/2009] => 30
//    [10/2009] => 31
//    [11/2009] => 30
//    [12/2009] => 31
//    [1/2010] => 30
//    [2/2010] => 29
//    [3/2010] => 31
//    [4/2010] => 30
//    [5/2010] => 31
//    [6/2010] => 30
//    [7/2010] => 31
//    [8/2010] => 31
//    [9/2010] => 30
//    [10/2010] => 31
//    [11/2010] => 30
//    [12/2010] => 31
//    [1/2011] => 30
//    [2/2011] => 29
//    [3/2011] => 31
//    [4/2011] => 30
//    [5/2011] => 31
//    [6/2011] => 30
//    [7/2011] => 31
//    [8/2011] => 31
//    [9/2011] => 30
//    [10/2011] => 31
//    [11/2011] => 30
//    [12/2011] => 31
//    [1/2012] => 30
//    [2/2012] => 29
//    [3/2012] => 31
//    [4/2012] => 30
//    [5/2012] => 31
//    [6/2012] => 30
//    [7/2012] => 16
//)

2/5/2008 - 7/16/2012
total days

Code: Select all

getElapsedDays('2/5/2008', '7/16/2012', false);
// returns:
// 1624
 
Post Reply