Log in

No account? Create an account

Previous Entry | Next Entry

there's more than one wrong way to do it

just a reminder: I like programming, but I'm a hack at it.

I wrote a calendar sort of thing that reported out what the first saturday of the month was, for the next 12 months. It worked great. PHP has this function called "strtotime" that converts a string (any text) to a timestamp... so I just put in "+1 month" so it would tell me the timestamp one month from now. I found the first saturday of that month. This was in a loop so it would do +2 months then +3 months and then so on.

It worked just fine until... yesterday! August 31st. Because one month after August 31st? Puts you in October! And two months from that is ALSO october... it was repeating even months and skipping the odd months.

It sucks to have a script that works one day, and then surprise one day it DOESN'T and you didn't change anything, you're like "I've been hacked!" but no... I haven't, I just don't think ahead all that well.

And to further my laziness I didn't even change my algorithm... just put in a trap that says if today is the 31st day of the month, tell me +1 months from yesterday.


( 3 comments — Leave a comment )
Sep. 1st, 2014 12:27 pm (UTC)
I am probably wrong, but your new +1 month from yesterday won't work in December, will it? Because there are 31 days in December and January. Oh hang on, you're saying first Saturday of the month, so it probably will. Am confusing myself was thinking of last day of the month. Ignore me
Sep. 2nd, 2014 10:40 am (UTC)
But will it work in January? i.e. does it know that one month from Jan 30 is still in February, and not in March?
Sep. 3rd, 2014 05:27 pm (UTC)
oh hell you're right.

points for collaboration!

well on that note, since I always felt like this algorithm was a hack I'm dumping the whole thing and doing it right...

$nextmonth = date('m',$timestamp) + $i; // where $i = months from now
$month = fmod($nextmonth,12); // month value of $i months from now
$year = floor($nextmonth/12)+date('y',$timestamp); // year value
( 3 comments — Leave a comment )

Latest Month

May 2019
Powered by LiveJournal.com
Designed by Tiffany Chow