I get some times from a database and I am trying to store them by month, using the epoch form.
So I could have some random times like:
1354120744
1328978360
1297388332
and for each one I want to find the epoch value which represents the 1st of that month
so for the last timestamp it would be:
1296518400 (using lovely epochconverter.com).
I know that I can use POSIX mktime to reduce the timestamp down to days, mins etc and change them accordingly etc as was the answer to a post I did a while ago: How to go back months in Perl taking account of different days in month?
The trouble I have with that is that while I can zero times, I’m worried that by changing the date the day of week will not match up, so essentially making an invalid date. This may be an unnecessary worry; I don’t know if it would make a difference having the wrong day of week if the times are validated.
I couldn’t see any other POSIX methods which seemed useful. Any help appreciated!
My first thought would be to use Time::Local to convert the
localtimeresults back to epoch, with the appropriate values forced to 0 or 1 as needed. Note that day of week is not among the input values it expects, so you don’t need to worry about what happens if you get that wrong.$start_of_month_epoch = timelocal(0, 0, 0, 1, $month, $year);