Jump to content

Adding variable Days to Variable Dates


Go to solution Solved by wscwt01,

Recommended Posts

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)
Link to comment
https://forums.phpfreaks.com/topic/301646-adding-variable-days-to-variable-dates/
Share on other sites

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.

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.

 

"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

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");
}

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.

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

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');
}

 

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

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');
}
  • Solution

 

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

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.