Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, This is a two parter. I have a timestamp field in my MySQL table. Since it stores this automagically I'm in luck!

I have a select box on my HTML form of 1 week, 1 month, 1 year as options. How do I get a Perl timestamp that's the same as MySQL? I guess that's the first question.

The 2nd part is how do I add time on to it? For example, if my boss selects 1 week from the form, I need to calculate just that.. Exactly 7 days from now.

Replies are listed 'Best First'.
Re: How to add time to timestamp?
by GrandFather (Saint) on Nov 21, 2013 at 02:03 UTC

    I'm too lazy to go look for what a MySQL time stamp looks like so I can't directly address part one. Part 2 is probably best answered using DateTime or Date::Manip (there are many other data and time related modules on CPAN too).

    True laziness is hard work
Re: How to add time to timestamp?
by boftx (Deacon) on Nov 21, 2013 at 02:23 UTC

    As GrandFather pointed out, DateTime can handle both the formatting and calculations you want. That said, you could also use Time::Piece (which I think is part of Perl Core now) as well. It has the advantage of being somewhat "lighter-weight" compared to D::T.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: How to add time to timestamp?
by Anonymous Monk on Nov 21, 2013 at 10:13 UTC
    Use the database to calculate. They're very good at date maths. I don't know the syntax for MySQL but in Postgres it's something like SELECT NOW() + interval '7 days';