Gossamer Forum
Home : Products : DBMan : Customization :

Calculating Dates

Quote Reply
Calculating Dates
I'm trying to add a field that will calculate the current day and and X number of days to it and store it in the database.

In sub add_record after
$status = &validate_record;

I put in
&calc_date


Subroutine as follows.

sub calc_date
{
$in{'Expdate'} = &get_date ( &date_to_unix ($in{'Date'}) + ($in{'Days'} * 86400);
}

It returns the current date.

What am I missing?


Thanks
Quote Reply
Re: Calculating Dates In reply to
You can try this, it might work, it might not I didn't look at the subs, just guessing what they do....

Code:
sub calc_date {
my ($temp_date);
$temp_date = &date_to_unix($in{'Date'});
$temp_date = $temp_date + ($in{'Days'}*86400);
$in{'Expdate'} = &get_date($temp_date);
}

Hope that helps...
Steve
Quote Reply
Re: Calculating Dates In reply to
You may need to update your sub get_date.

Go to http://www.jpdeni.com/dbman/Mods/changes.txt and pick up my "Misc changes" mod. Replace your sub get_date with the one in the mod. Then give it a try.

Your code looks good.




------------------
JPD





Quote Reply
Re: Calculating Dates In reply to
Worked PERFECT!

Thanks.
Quote Reply
Re: Calculating Dates In reply to
Hi!
I'm trying something similar and having no luck at all.

I need to be able to supply the date and have another field filled by the calculation (date supplied + 35 days) or 5 weeks, if that's easier.

I've tried every suggestion listed at http://webmagic.hypermart.net/dbman/dates.htmto no avail.

Here is what I have currently: (placed in the sub validate_record portion of the db.cgi)
Code:
$in{'DPD'} = ($in{'EPD'} + 3024000);

which results in 3024002
I have also tried
Code:
$in{'DPD'} = &get_date (&date_to_unix ($in{'EPD'} + 3024000);
which displayed TODAY'S DATE.

Somewhere in my attempts I repeatedly got 01/01/1970 as a result.

If anyone has any suggestions, I would be most grateful!

Thanks,
keienb


Quote Reply
Re: Calculating Dates In reply to
request:
I need to be able to supply the date and have another field filled by the calculation (date supplied + 35 days) or 5
weeks, if that's easier

I use a field to automatically set a remove date when a classified ad is placed. I set this within my .cfg file using the code below:

'Remove_by' => [26,'date',15,15,0,&get_date(time + 7776000),'\d{2}\/\d{2}\/\d{4}'], ### sets for 90 days after posting date

When an add is placed this field is automatically set to make the date appear in the Remove_by field for 90 days later. Simple and it works!

You should just be able to replace "7776000" with the number of days you want to increment the date.

Hope this helps

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: Calculating Dates In reply to
Thanks for your reply. Yes, what you suggested should work fine... but it only works from XX days from "get_date" (or, today) ... I need it to function from the date that is input in another field - whether that date is today or not.

I have been beating my head against the wall for the last several hours - I've downloaded and installed "Date::Manip" from CPAN - and I have managed the calculation, BUT my results are now displayed as 2001-01-03:15:41:53
which I can't seem to manipulate. I don't want the time displayed at all ... and I would like to see the results displayed the same way that I put in the date field: 01-JAN-2001

Any more ideas???

Thanks!

keienb

I've just been trying again - and used your suggestion - cuz I gotta have it working (even it it's not what I'm looking for) and I've run into similar problems. I've downloaded a new copy of dbman and started over (thinking that might help) and I've copied your "Remove_by" line and re-number for my cfg file - repeatedly I get "invalid format" and "invalid date format." If I tell it that field is "numer" instead - no errors, but it displays the +number (in my case " 3024000 " PLUS the day number for today - giving me a result of 3024003.

HELP!?!?!?! I don't know what I've done wrong, but I'm getting nowhere quick :(

Thanks again!

Quote Reply
Re: Calculating Dates In reply to
I think you may have better luck using a field which you enter the number of days (i.e 35) that you want to be calculated. This code given above:

sub calc_date {
my ($temp_date);
$temp_date = &date_to_unix($in{'Date'});
$temp_date = $temp_date + ($in{'Days'}*86400);
$in{'Expdate'} = &get_date($temp_date);
}

the field 'Days' is a field that holds the number of days or timeframe to be used in the calculation. Perhaps setting up your database so that rather than basing it on a date may help.

Check out the following reference in the FAQ noted below as it contains two little subs that can be used to accomplish what you need.

I realize the main mod is for autodelete, but the other subs are what you are actually looking for. Just don't use the autodelete portion, just the subs which will calculate the dates for you.

http://webmagic.hypermart.net/dbman/autodel.htm
and choose "Autodelete for multiple/relational databases"

The other threads listed may also provide help.

Hope this helps


Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: Calculating Dates In reply to
Thanks Lois!!!
That did the trick Smile

keieb

Quote Reply
Re: Calculating Dates In reply to
Lois -

Now that I finally have what I asked for .... I'm looking to 'tweak' it (isn't that always the case)

Every attempt I have made to use the 'get_date' and 'date_to_unix' subs that output DD-MMM-YYYY format, I get errors. If I use the standard MM/DD/YYYY format it works great.

Any suggestions on how to make this work?

Thanks again!

keienb

Quote Reply
Re: Calculating Dates In reply to
As JPDeni stated in a thread above:

You may need to update your sub get_date.

Go to http://www.jpdeni.com/dbman/Mods/changes.txt and pick up my "Misc changes" mod. Replace your sub
get_date with the one in the mod. Then give it a try.

You have to make sure that your get_date and date_to_unix match exactly in the format display.

You may want to view the threads in the FAQ noted below under "Dates" and see if you can find how it would be with the format you are looking for. The various autodelete mods may also provide the different display options.

So glad you got the calculations working Smile

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: Calculating Dates In reply to
I guess I didn't explain myself. I've updated both subs - and the date functions work just fine (old & new) depending on which sub is in place.

The problem arises when I attempt to use the sub calc_date.
Somehow, it appears as tho the calc_date is requiring the "day" to be the second entry? if that's possible?
Cuz with the DD-MMM-YY format working (for normal date display), it only gives me "1" for the calc_date.

Anyway, I'll keep trying!

Thanks,

keienb