I-AM-OBODO Posted July 14, 2015 Share Posted July 14, 2015 hi all. having a hard time figuring this out. i have: Payment frequency: weekly(7days) twice-monthly(14days) monthly(30days)First payday: 8thNext payday: first payday + payment frequency Date of transaction: 2015-07-14 (today) Date difference: difference between date of transaction(today) and next payday: Due date ? For argument sake: Payment frequency = 30(Monthly) First payday = 8th Next payday = 38th ( 8th of next month) Date of transaction: 2015-07-14 (today) Date difference = 8th next month - today My problem is how to express 8th of next month in date format. Thanks $first_payday = 8; $today = date('Y-m-d'); if($pay_frequency == "Monthly"){ $new_pay_frequency = 30; }elseif($pay_frequency == "Twice_monthly"){ $new_pay_frequency = 14; }elseif($pay_frequency == "Weekly"){ $new_pay_frequency = 7; } echo "Payment frequency: " . $new_pay_frequency; echo "<br>"; echo "First payday: " . $first_payday; echo "<br>"; $next_payday = $new_pay_frequency + $first_payday; echo "Next payday: " .$next_payday; Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 14, 2015 Share Posted July 14, 2015 Rather than just adding 30 days, you should increment the month by one instead. Not every month has 30 days. Now the logic is simple - simply add one to the current month number. You'd need end-of-the-month and end-of-the-year logic. For example, if your pay day was on the 30th of January, adding one to the month would be the 30th of February - but that doesn't exist. So you'd just come down to that month's last day. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 15, 2015 Author Share Posted July 15, 2015 Rather than just adding 30 days, you should increment the month by one instead. Not every month has 30 days. Now the logic is simple - simply add one to the current month number. You'd need end-of-the-month and end-of-the-year logic. For example, if your pay day was on the 30th of January, adding one to the month would be the 30th of February - but that doesn't exist. So you'd just come down to that month's last day. ok thanks. will put your suggestion into consideration but that is not my problem just yet. i need to express next payday in dates. and mind you the payment frequency could be weekly or twice weekly. thanks Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 15, 2015 Share Posted July 15, 2015 You can use strtotime() to do math on a date. Or, you can also use DateTime to do the same. Quote Link to comment Share on other sites More sharing options...
maxxd Posted July 15, 2015 Share Posted July 15, 2015 Set up the frequency as DateInterval() objects, then feed them to DateTime::add() as necessary. DateTime::add() - http://php.net/manual/en/datetime.add.php DateInterval() - http://php.net/manual/en/class.dateinterval.php Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 16, 2015 Author Share Posted July 16, 2015 Thanks all. I ended up using php date/time function instead of mysql. I have my logics right just as i stated earlier but i have two issues. (1) i'd like to get rid of the + sign in from of date_diff (2) just as scootstah noted; not all months are 30 days, there February and others that have 31 days. How do i get it so that it works for all the months. Thanks my code $pay_frequency = "Monthly"; $first_payday = 10; //date of transaction $today = date('Y-m-d'); if($pay_frequency == "Monthly"){ $new_pay_frequency = 30; }elseif($pay_frequency == "Twice_monthly"){ $new_pay_frequency = 14; }elseif($pay_frequency == "Weekly"){ $new_pay_frequency = 7; } // make date for current month $make_date = date('Y-m-01'); //payment frequency echo "Payment frequency: " . $new_pay_frequency ; echo "<br>"; //first payday echo "First payday: " . $first_payday; echo "<br>"; //add payment frequency to first payday to get next payday in days $next_payday = $new_pay_frequency + $first_payday; echo "Next payday(in days): " . $next_payday; echo "<br>"; //get next payday in date format $date = date_create($make_date); date_add($date, date_interval_create_from_date_string($next_payday.'days')); $nu = date_format($date, 'Y-m-d'); echo "Next payday date: " . $nu; echo "<br>"; // difference between date of transaction and next payday $datetime1 = date_create($today); $datetime2 = date_create($nu); $interval = date_diff($datetime1, $datetime2); echo "Date difference: ". $interval->format('%R%a days'); Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2015 Share Posted July 16, 2015 1 ) The "%R" in the format string is causing the + sign to be output. If you don't want it, remove it. 2 ) If you use DateTime object to add 1 month you risk missing a month eg $dt = new DateTime('2015-01-31'); $dt->add(new DateInterval('P1M')); echo $dt->format('Y-m-d'); // 2015-03-03 (jumps from Jan to Mar) If you use SQL mysql> SELECT '2015-01-31' + INTERVAL 1 MONTH as next_pay; +------------+ | next_pay | +------------+ | 2015-02-28 | +------------+ 1 Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 16, 2015 Share Posted July 16, 2015 @obodo, I'm not sure what the purpose of your code is, but it does not match reality. I'm sure there are some exceptions out there, but how you are calculating the pay periods is not what is done in the real world. Companies that pay once a month, don't set the pay periods up to be 30 days apart. They set up the payday to be on the same numerical date each month. For example, payday would be on the 5th of each month. However, they would also have rules to account for when the 5th lands on a weekend or a holiday. For paydays that are every every week, or every two weeks, those would be on the same day of the week every week (or two) with adjustments for holidays. Note that with this type of pay periods, there will be months with five paydays (for weekly) or three paydays (for bi-weekly) For paydays that are twice a month, they are set up the same as the monthly pay period. They will be on two specific dates each month (e.g. the 1st and the 15th). The logic you are using is not accurate and is more complicated than reality, IMHO. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 16, 2015 Author Share Posted July 16, 2015 @obodo, I'm not sure what the purpose of your code is, but it does not match reality. I'm sure there are some exceptions out there, but how you are calculating the pay periods is not what is done in the real world. Companies that pay once a month, don't set the pay periods up to be 30 days apart. They set up the payday to be on the same numerical date each month. For example, payday would be on the 5th of each month. However, they would also have rules to account for when the 5th lands on a weekend or a holiday. For paydays that are every every week, or every two weeks, those would be on the same day of the week every week (or two) with adjustments for holidays. Note that with this type of pay periods, there will be months with five paydays (for weekly) or three paydays (for bi-weekly) For paydays that are twice a month, they are set up the same as the monthly pay period. They will be on two specific dates each month (e.g. the 1st and the 15th). The logic you are using is not accurate and is more complicated than reality, IMHO. what then would you propose? Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 16, 2015 Author Share Posted July 16, 2015 1 ) The "%R" in the format string is causing the + sign to be output. If you don't want it, remove it. 2 ) If you use DateTime object to add 1 month you risk missing a month eg $dt = new DateTime('2015-01-31'); $dt->add(new DateInterval('P1M')); echo $dt->format('Y-m-d'); // 2015-03-03 (jumps from Jan to Mar) If you use SQL mysql> SELECT '2015-01-31' + INTERVAL 1 MONTH as next_pay; +------------+ | next_pay | +------------+ | 2015-02-28 | +------------+ the sql you suggested takes care for only when month is the pay frequency. what can be done on sql to accommodate for both bi-weekly, weekly and monthly? thanks Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted July 17, 2015 Solution Share Posted July 17, 2015 specify interval based on N days mysql> SELECT '2015-01-31' + INTERVAL 7 DAY as next_pay; +------------+ | next_pay | +------------+ | 2015-02-07 | +------------+ or N weeks mysql> SELECT '2015-01-31' + INTERVAL 2 WEEK as next_pay; +------------+ | next_pay | +------------+ | 2015-02-14 | +------------+ Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 19, 2015 Author Share Posted July 19, 2015 specify interval based on N days mysql> SELECT '2015-01-31' + INTERVAL 7 DAY as next_pay; +------------+ | next_pay | +------------+ | 2015-02-07 | +------------+ or N weeks mysql> SELECT '2015-01-31' + INTERVAL 2 WEEK as next_pay; +------------+ | next_pay | +------------+ | 2015-02-14 | +------------+ Thanks a zillion dozen times. I have modified my code to use sql but i read somewhere that sql dates are not to be trusted! which is better? thanks Quote Link to comment 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.