I have table which has the table field name:
- Start Date
- Duration Count (e.g. 1, 2 or 3)
- Duration Name (e.g. Daily, Monthly, Yearly)
- End Date

How do I code in PHP to calculate the end date from a particular start date?

E.g. The start date is on 1st January 2013, and duration count is 1, duration name is yearly.
Which actually meant to be 1 year duration. How do I code in order to get the answer of
31st December 2013 in my end date column?

Example Formula: $StartDate + $DurationCount & $DurationName = $EndDate?

Member Avatar for diafol

Use DateTime. Here's general usage:

$interval = 'P' . $DurationCount . $DurationName; //DurationName = D or M or Y

$date = new DateTime($StartDate); //$StartDate in Y-m-d format
$date->add(new DateInterval($interval));
echo $date->format('Y-m-d') . "\n";

You could add the year, then subtract a day ( using $date->sub() )

Hmmm... I have a question. If were to use the above general usage.
What about if it is the month February?

For example, there is an invoice billing which starts on 31st January 2013 and the bill supposed to due after one month? If the February doesn't falls in a leap year, the bill will due on 1st March 2013. If the February falls on a leap year, the bill should due on 2nd March.

Any idea of how to calculate the consistency for monthly, yearly due?

Member Avatar for diafol

Why will the bill be due on 1st of March or 2nd ? The bill should be set for the last but one day of the month? (27th for normal Feb, 28th for leap year). Well, that's the case you gave for your first post [1/1/2013 -> 31/12/2013]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.