uncat_myself Posted July 24, 2013 Share Posted July 24, 2013 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 of31st 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 whenwhether 2013 is a lear year or non leap year. Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/ Share on other sites More sharing options...
Muddy_Funster Posted July 24, 2013 Share Posted July 24, 2013 do it as part of the mysql query - use DATE_ADD() Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441908 Share on other sites More sharing options...
uncat_myself Posted July 24, 2013 Author Share Posted July 24, 2013 Thank you for your solution. Is there a way which can be only coded in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441909 Share on other sites More sharing options...
Muddy_Funster Posted July 24, 2013 Share Posted July 24, 2013 well PHP has a simmilar function, but I don't know why you wouldn't want to do it as part of the query - have a look at date_add() Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441910 Share on other sites More sharing options...
AbraCadaver Posted July 24, 2013 Share Posted July 24, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441937 Share on other sites More sharing options...
Psycho Posted July 24, 2013 Share Posted July 24, 2013 (edited) 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 July 24, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441941 Share on other sites More sharing options...
Psycho Posted July 24, 2013 Share Posted July 24, 2013 (edited) 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 July 24, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/280452-how-to-get-end-date-from-adding-start-date-with-duration-count/#findComment-1441953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.