Working with Dates

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
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Working with Dates

Post 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!
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Working with Dates

Post 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
}
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Working with Dates

Post 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
Post Reply