in reply to Add A Number of Days to Today's Date

Perl finds it all much easier if you deal with dates as the number of seconds since 00:00 Jan 1 1970 (this number, incidently, has just passed one billion). The function time gives this value for the current time and the timelocal function (from the Time::Local module) can convert any date or time into this format.

You can then use the POSIX::strftime to convert that value into a human-readable string. Here's an example:

use POSIX 'strftime'; my $now = time; my $then = $now + (20 * 86_400); # 86,400 seconds in a day my $then_str = strftime('%d %B %Y', localtime($then)); print "In 20 days time it will be $then_str\n";

Of course, it's possible to write that more compactly :)

use POSIX 'strftime'; print "In 20 days time it will be ", strftime('%d %B %Y', localtime(time + 20 * 86_400)), "\n";
--
<http://www.dave.org.uk>

"The first rule of Perl club is you don't talk about Perl club."

Replies are listed 'Best First'.
Re: Re: Add A Number of Days to Today's Date
by rline (Initiate) on Sep 29, 2001 at 04:55 UTC
    Hey, Thanks y'all for your time and effort in replying to my question! I really appreciate it. OK. So I understand how to get the date in x days time. How can I automatically insert the variable $then_str into my flatfile database? The database is appended every time a user fills out a form. So there are about 20 fields which are appended. How do I automatically insert the variable $then_str next to, say, the 15th field in the database? So the script needs to pull the 20 user-filled-in values, and then add my created $then_str value next to the 15th value, to make a total of 21 values in the database? Thanks in advance!