Showing posts with label floor. Show all posts
Showing posts with label floor. Show all posts

Monday, December 14, 2015

Chunk IPv4 range into multiple sub-chunks

One of my non-programmer-but-in-IT friends came to me last week, asking help to chunk a range of IPv4 addresses into N small chunks. And, here's the PHP code I came up with within 10 mins.

What it simply does is, explode the input range, and convert start and end IP address into long, using ip2long(). Then, using range() expand it into a whole list. Once that is done, then decide number of elements that should go into each chunk - remember, the optional argument to this function tells how many chunks are to be prepared. the formula is there on code line number 11.

Once you have this number, call array_chunk() to create chunks. Finally, iterate over them, get start and end IP of each of them (which are still long numbers), convert them back to IPv4 address using long2ip() and create the range string for them.

Do you find it useful? Copy it! Have any suggestion, let me know in the comment!!

Saturday, April 25, 2015

Week of the Month

In my project work, I needed to find out whether the given date falls in 1st, 2nd, 3rd, 4th or 5th week of the given month. As usual, I googled for this solution and found many functions. http://www.hotscripts.com/forums/php/37900-week-number-month.html, http://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month and http://mybroadband.co.za/vb/showthread.php/66311-Calculating-the-week-in-the-month-(PHP) are some of them. But, being lazy, I thought those functions were too complex, and there could be a simpler solution to this. So I spent some time and came up with this function. Here is the code.