Get number of days in month with PHP

Problem: get number of days in certain month with PHP, if $m – is a month and $y – is an year.

First solution, hacker-style (source):

(31 - (($m 1) % 2) - ((($m == 2) << !!($y 4)))) ?>

This solution doesn’t work properly for years divisible by 100, and non-divisible by 400 (years 1900 and 2100 are not leap)

Second solution, canonical:

date("t",mktime(0,0,0,$m,1,$y));

This solution works only for dates which can be presented as unix timestamps, therefore it’s not good (depends on PHP version and OS.)

Third solution, proper:

function days_in_month($y,$m)
{//by val petruchek http://petruchek.com
    
$d 31;
    while(!
checkdate($m,$d,$y)) $d--;
    return 
$d;
}

This solution is based on built-in PHP function checkdate() which validates Gregorian date without unix timestamps.

Leave a comment

Test before you send

This summer was really hot for my hosting provider. Here is summary of emails I’ve received from them:
Jun 11:

On June 27, 2010, between 06:00am and 10:00am CST, our Development and Database teams will be performing several upgrades of the infrastructure that support Orbit.

Jun 16:

On June 13, 2010, between 01:00am and 02:00am CST, our Product Support team will perform a reboot of ******.**.***** which provides load balancing services to you.

1 hour later:

Today you received a maintenance notification dated for June 13, 2010. The incorrect message was sent out and the correct will be sent at approximately 2pm CST.

5 minutes later:

On June 20, 2010, between 06:00am and 10:00am CST, our Networking group will be performing code upgrades on ******.***** to resolve several bugs.

3 hours later:

We apologize for the recent notifications which have had incorrect information. The correct change window for this maintenance is from 00:01 to 05:00 CT rather than 6:00 to 10:00 CST as previously sent.

The correct text should have read:

On June 20, 2010, between 00:01am and 05:00am CT, our Networking group will be performing code upgrades on *****.***** to resolve several bugs.

Jun 18:

On June 27, 2010, between 06:00am and 10:00am CST, our Development and Database teams will be performing several upgrades of the infrastructure that support Orbit.

Jun 25:

On June 27, 2010, between 06:00am and 10:00am CST, our Development and Database teams will be performing several upgrades of the infrastructure that support Orbit.

Jul 9 and Jul 15:

Scheduled Maintenance
June 24, 2010

Jul 19:

Emergency Maintenance
July 20, 2010

Well, my first CRM emailing script sent same email to couple of customers 5 times before I realized something is wrong in my coding. Since that, I have extremely strict testing policy for every application which sends out mass emails.

The easiest way to do a quick test is replacing actual customer’s email address with special test accounts in the test mode and counting number of emails received as well as validating their contents (proof reading + %username% quick tag replacements and such).

It doesn’t take much time but prevents you from annoying your customers – “hey, what kind of IT company are they if they can’t even handle their own mass mailing properly?”

Leave a comment

What is mod_rewrite

I love these mod_rewrite epigraphs:

The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.

Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.

Leave a comment

Invalid markup

Valid markups are all alike; every invalid markup is invalid in its own way.
(Leo Tolstoy, Anna Karenina)

Speaking about impossibility to repair invalid markup automatically for any given markup.

Leave a comment

Format file size

Code snippet to output number of bytes with unknown capacity:

function format_filesize($bytes
{
    if (
$bytes == 0)
        return 
'0.00 B';

    $s = array('B''Kb''MB''GB''TB''PB');
    
$e floor(log($bytes)/log(1024));

    return sprintf('%.2f '.$s[$e], ($bytes/pow(1024floor($e))));
}

I don’t know the author of this code.

1 Comment

Curl — change your IP address

If you have multiple IP addresses and you want to make curl request using non-default IP, you can do it by setting CURLOPT_INTERFACE option:

curl_setopt ($chCURLOPT_INTERFACE'11.22.33.44');

This will work only if you really have that another IP. If you do not have extra IPs, and your only IP is banned already, you need proxies.

If you want to forge your IP address, curl is wrong tool, too.

Leave a comment

Slow FTP Login

Angry users sent me a link to discussion of what can cause Proftpd delay during authorization.

So I’ve decided to fix it.

Added UseReverseDNS off to config file — didn’t help.

Added GoogleDNS (8.8.8.8) to resolv.conf — didn’t help.

Added IdentLookups off to proftpd.conf — helped.

Thanks to Ray Dehler.

When IdentLookups is turned on it means that Proftpd is trying to identify the remote username via ident (RFC1413) protocol.

Leave a comment

Easter Egg from Last.fm

What a lovely easter egg from Last.fm developers in their robots.txt file:

Disallow: /harming/humans
Disallow: /ignoring/human/orders
Disallow: /harm/to/self

Hint.

Leave a comment

OK, let’s try

It looks like I was able to make WordPress work from single installation on two different domains in different languages without touching its core files.

I do not want to touch its core files because I want to be able to upgrade automatically.

Anyways, I do not see any invalid links. If you find invalid link, please post it in comments.

Now I’m gonna try to blog into single blog but in two languages, if I have enough time.

Leave a comment