Jump to content

How to get End Date from Adding Start Date with Duration Count?


uncat_myself

Recommended Posts

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?

 

Noticed that I'm facing the problem when it comes to monthly issues. For example is an invoice bill 
starts on 31st January 2013, after one month the bill supposed to be due in March. It matters when
whether 2013 is a lear year or non leap year.

Link to comment
Share on other sites

Assuming the date is a Unix timestamp, something like this:

$durations = array('Daily'=>'day', 'Monthly'=>'month', 'Yearly'=>'year');
//assume $duration_name = something already like 'Daily'
$duration_name = $durations[$duration_name];
$end_date = strtotime("+$duration_count $duration_name", $start_date);

 

Link to comment
Share on other sites

Why do you have the fields Duration Count & Duration Name AND an End Date field? Are you wanting to use PHP to determine the end date for the purpose of populating a value for that field? If so, you don't need it since you can always get the end date when you query the table as Muddy_Funster stated. It's a bad idea to try and store applicative information since it creates the opportunity for the data to get out of sync since you have to always make sure you update both sets of values.

 

But, even if you are going to store that value, you should still use MySQL to populate the value, which can be done very simply in the INSERT query.

INSERT INTO table_name
    (start_date, duration_count, duration_name, end_date)
VALUES ('$startDate', '$durationCount', '$durationName', DATE_ADD($startDate, INTERVAL $durationCount $durationName))
Edited by Psycho
Link to comment
Share on other sites

I need to correct a statement above. I think I had a typo and then used auto-correct, but selected the wrong value:

It's a bad idea to try and store applicative duplicative information since it creates the opportunity for the data to get out of sync since you have to always make sure you update both sets of values.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.