wscwt01 Posted July 26, 2016 Share Posted July 26, 2016 Hi, I have an application where I have a field containing an Issue Date and a field which contains a Number of Days. I need to add the Number of Days to the Issue Days to get a Expiry Date.(The bit in bold) My code as it stands which is not quite working is: - if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $values['ExpiryDate'] = date('Y-m-d',strtotime($values['IssueDate'])) + (60*60*24*$values['Workings3'])); }; return true; Any guidance would be much appreciated Working3 is field containing days i.e. 730 (2 years) Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/ Share on other sites More sharing options...
maxxd Posted July 26, 2016 Share Posted July 26, 2016 Admittedly, I get a bit concerned when I see variables named incrementally, but I'm going to assume you're using them as examples. So, to answer the question, you can use a DateTime object. //stub values for testing purposes $tz = new DateTimeZone('America/New_York'); $values['IssueDate'] = new DateTime('now', $tz); $values['Workings1'] = 'D'; $values['Workings2'] = 'tomorrow'; $values['Workings3'] = '730'; //actual functionality if ($values['Workings1'] == "F") { $values['ExpiryDate'] = new DateTime($values['Workings2'], $tz); } else if ($values['Workings1'] == "D") { $values['ExpiryDate'] = clone($values['IssueDate']); $values['ExpiryDate']->modify("+{$values['Workings3']} days"); } print("<p>This is the expiration date: {$values['ExpiryDate']->format('Y-m-d')}</p>"); This is obviously a less than perfect solution as there's no error checking (what if the 'Workings3' index contained a number of years or seconds? Or 'Bob'?), but it should point you in the right direction. Also, if it's possible for 'Workings1' - and I have to stress again that I really hope that's not what you're calling your indexes - to contain anything other than 'F' or 'D', I'd recommend looking at a switch() statement instead of a spaghetti pile of else if()'s. Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535099 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 Admittedly, I get a bit concerned when I see variables named incrementally, but I'm going to assume you're using them as examples. So, to answer the question, you can use a DateTime object. //stub values for testing purposes $tz = new DateTimeZone('America/New_York'); $values['IssueDate'] = new DateTime('now', $tz); $values['Workings1'] = 'D'; $values['Workings2'] = 'tomorrow'; $values['Workings3'] = '730'; //actual functionality if ($values['Workings1'] == "F") { $values['ExpiryDate'] = new DateTime($values['Workings2'], $tz); } else if ($values['Workings1'] == "D") { $values['ExpiryDate'] = clone($values['IssueDate']); $values['ExpiryDate']->modify("+{$values['Workings3']} days"); } print("<p>This is the expiration date: {$values['ExpiryDate']->format('Y-m-d')}</p>"); This is obviously a less than perfect solution as there's no error checking (what if the 'Workings3' index contained a number of years or seconds? Or 'Bob'?), but it should point you in the right direction. Also, if it's possible for 'Workings1' - and I have to stress again that I really hope that's not what you're calling your indexes - to contain anything other than 'F' or 'D', I'd recommend looking at a switch() statement instead of a spaghetti pile of else if()'s. Hi maxxd thanks so much for taking the time to look at my problem. I'm afraid that when I ran it it errored. I am using a code building product called PHP Runner and it doesn't seem to recognise the function "clone" which is where it errored. Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535126 Share on other sites More sharing options...
Barand Posted July 27, 2016 Share Posted July 27, 2016 "clone" doesn't use parentheses. EG $copy = clone $obj; Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535131 Share on other sites More sharing options...
maxxd Posted July 27, 2016 Share Posted July 27, 2016 "clone" doesn't use parentheses. EG $copy = clone $obj; I did not know that. Strange thing is, it worked when I tested it here in 7.0.4... Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535132 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 "clone" doesn't use parentheses. EG $copy = clone $obj; Thanks for getting back so quick but unfortunately Fatal error: __clone method called on non-object in /home/z14auch/public_html/vouchers/vouchers/include/tblVouchers_events.php on line 35 Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535133 Share on other sites More sharing options...
Barand Posted July 27, 2016 Share Posted July 27, 2016 What is the code where you (try to) create the object that is being cloned? Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535134 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 It is located in a "Before record Add" event located in an Add New Record page.I create the code in PHPRunner which is then compiled and uploaded to live server. What is the code where you (try to) create the object that is being cloned? Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535135 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 It is located in a "Before record Add" event located in an Add New Record page. I create the code in PHPRunner which is then compiled and uploaded to live server. The actual code is: - if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $values['ExpiryDate'] = clone $values['IssueDate']; $values['ExpiryDate']->modify("+{$values['Workings3']}) days"); } Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535138 Share on other sites More sharing options...
Barand Posted July 27, 2016 Share Posted July 27, 2016 What is your code where you create $values['IssueDate'] ? Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535139 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 What is your code where you create $values['IssueDate'] ? IssueDate is a Field in the Form which autopopulates with now() Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535140 Share on other sites More sharing options...
maxxd Posted July 27, 2016 Share Posted July 27, 2016 IssueDate is a Field in the Form which autopopulates with now() In which case it's not an object. Note in the code I provided $values['IssueDate'] is a DateTime() object. Convert $values['IssueDate'] to a DateTime object and try it again. Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535142 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 In which case it's not an object. Note in the code I provided $values['IssueDate'] is a DateTime() object. Convert $values['IssueDate'] to a DateTime object and try it again. Hi guys, I think I am getting confused now as probably so are you, so just to clarify This bit of the code works absolutely fine: - { if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } return true; Where "Workings1" contents is populated depending on what ever Voucher code is selected from a dropdown elsewhere on the Form. It will only ever be "F" or "D" Where "Workings2" contents is populated depending on what ever Voucher code is selected from a dropdown elsewhere on the Form. It will always be a date in shortform. Where "ExpiryDate" is empty field Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535145 Share on other sites More sharing options...
Barand Posted July 27, 2016 Share Posted July 27, 2016 try if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $dateObj = new DateTime($values['IssueDate']); $dateObj->modify("+{$values['Workings3']}) days"); $values['ExpiryDate'] = $dateObj->format('Y-m-d'); } Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535148 Share on other sites More sharing options...
wscwt01 Posted July 27, 2016 Author Share Posted July 27, 2016 try if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $dateObj = new DateTime($values['IssueDate']); $dateObj->modify("+{$values['Workings3']}) days"); $values['ExpiryDate'] = $dateObj->format('Y-m-d'); } Thanks this is result DateTime::modify(): Failed to parse time string (+730) days) at position 4 ()): Unexpected character Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535149 Share on other sites More sharing options...
Barand Posted July 27, 2016 Share Posted July 27, 2016 I copied your extra ")" if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $dateObj = new DateTime($values['IssueDate']); $dateObj->modify("+{$values['Workings3']} days"); $values['ExpiryDate'] = $dateObj->format('Y-m-d'); } Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535150 Share on other sites More sharing options...
Solution wscwt01 Posted July 27, 2016 Author Solution Share Posted July 27, 2016 I copied your extra ")" if ($values['Workings1'] == "F") { $values['ExpiryDate'] = $values['Workings2']; } else if ($values['Workings1'] == "D") { $dateObj = new DateTime($values['IssueDate']); $dateObj->modify("+{$values['Workings3']} days"); $values['ExpiryDate'] = $dateObj->format('Y-m-d'); } Fantastic............. Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/#findComment-1535151 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.