Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Getting previous dates from the current date

by FitTrend (Pilgrim)
on Mar 29, 2005 at 16:13 UTC ( #443170=note: print w/ replies, xml ) Need Help??


in reply to Getting previous dates from the current date

Pretty easy. There are 86400 seconds in a day.

$time = time() - (86400 * $numOfDays); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tim +e); $mon++; $year = $year + 1900;
or
$time = time() - (86400 * $numOfDays); $realTime = localtime($time); print scalar $realTime;


Comment on Re: Getting previous dates from the current date
Select or Download Code
Re^2: Getting previous dates from the current date
by Smylers (Pilgrim) on Mar 29, 2005 at 17:29 UTC
    Pretty easy.

    Not in the general case it isn't. Date and time calculations seem to be one of those things that sound misleadingly simple, but actually turn out not to be.

    There are 86400 seconds in a day.

    Not always. This code will not work correctly if it spans changing the clocks for daylight-saving (see a previous reply above for a detailed example).

    Smylers

Re^2: Getting previous dates from the current date
by FitTrend (Pilgrim) on Mar 29, 2005 at 18:26 UTC

    You are right. This code does not account for dst. Using the same code and adding 10 days (which we change the clocks next weekend), it will output 10 days and 1 hour.

    To counter act this, determining the first weekend and april and last weekend in october will allow a user to adjust for this. Or you can use a module.

      To counter act not accounting for DST, determining the first weekend and april and last weekend in october will allow a user to adjust for this.

      Well it would do, so long as you're in a place that uses those dates. But it would still be wrong in the EU, for example, where the clocks go back on the last weekend of March, not the first one in April.

      And presumably it'd be even more wrong in the southern hemisphere, where I'm guessing the clocks change in the opposite direction?

      Now you could do lots of research and then allow for all these things, or ...

      Or you can use a module.

      Yes. Use a module. Modules are good!

      Smylers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://443170]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-25 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls