Page 1 of 1

Working with Dates

Posted: Sat Nov 06, 2010 2:06 pm
by s992
I'm working on some invoicing software where I need to store an array of dates(the customer's bill cycle). Customers are generally billed quarterly, but reports are sent out every month. When displaying the report, I need to do some conditional formatting depending on if this invoice reflects a bill cycle month or not.

I'm currently storing the dates in an imploded array of date stamps created using mktime(0,0,0,$month,1,date('Y')) - I was planning on just comparing the date stamps to whatever the date stamp for the invoice was. Then, it occurred to me that any time the year changes those date stamps will be obsolete. Should I just store their bill cycle months as month names and compare the strings with in_array()?

Code: Select all

$billcycle = array('January','April','July','October');
$invoicedate = mktime(0,0,0,1,1,date('Y')); // Invoice Date: January 1 of this year
$comparedate = date('F',$invoicedate); // Get just the month(January) from $invoicedate

if(in_array($comparedate,$billcycle)) {
	// perform conditional formatting
} else {
	// don't
}
I just wrote the above code as an example, so there may be syntax errors.

Is there a better way to do this? Seems kludgy. Thanks in advance for your input!

Re: Working with Dates

Posted: Sun Nov 07, 2010 2:33 am
by mikecampbell
Personally, I think the code looks fine. An alternative would be to get the month number by calling date() with 'n' instead of 'F', then doing this.

Code: Select all

if ((($comparedate-1) % 3) == 0) {
        // perform conditional formatting
} else {
        // don't
}

Re: Working with Dates

Posted: Sun Nov 07, 2010 11:41 am
by s992
Hey Mike, thanks for your input. I guess if it works I shouldn't beat my head against a wall trying to find another way! :D