Clear questions and runnable code
get the best and fastest answer
Re: Use 'strftime' to calculate a date/time in the Past.by mmartin (Monk)
|on Sep 17, 2012 at 19:39 UTC||Need Help??|
Ok, so I was able to figure this out.
So I used the same exact formula from my OP and did the total seconds into the past minus 'time'.
This was almost exactly what I wanted, but no matter what I put for a length into the past the
output was ALWAYS showing 4 Hours off from what it should have been...
I then realized that I should have been using "localtime()" instead of "gmtime()". I switched them
up and now its working like a charm!!
Here's how to do it if anyone ever gets stuck trying to calculate a Date AND Time into the past:
For Example --> I want "2 Days, 12 Hours, 1 Minute, and 20 Seconds" into the past...
From my Linux CMD Prompt: FYI... The integer '216080' is the total seconds into the past
### This will print the date and Time in a readable format (i.e. mm/dd/yyy hh:mm:ss in 24-Hour Format)...
perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%m/%d/%Y %T", localtime( time-216080 )); print "$_n_days_ago\n"'
### Now what I wanted for MY output is (i.e. "$seconds, $minutes, $hours, $mday, $month, $year")
# I wanted it like this so I could get the UNIX Timestamp of that Past Date and Time by using those values from its output...
perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%S, %M, %k, %d, %m, %Y", localtime( time-216080 )); print "$_n_days_ago\n"'