Reading STDERR output in PHP

Recently I was working on a command line PHP script. I know that there are better ways to do things, but I didn’t want to use any other language, since most developers in our company know mainly PHP.

The script exec’s MySQL command to execute a batch of SQL queries. I also wanted the script to mail the error messages. mysql redirects error messages to STDERR - but STDERR is by default not available in exec’s output argument.

Walkaround is simple:

<?php

exec('your_command 2>&1');

?>

Works!

ISO 8601 compliant week numbers

Last week one of our customers noticed that the Calendar module in our CRM application shows incorrect week numbers. Since it was a really old module we were pretty sure that they were correct - isn’t the PHP’s date function supposed to do all the dirty work for us?

Later on we came up with Polish government’s norm (suggestion, to be exact) that states that the first week of the year is the first week which has at least 4 days in the year (and since it’s ISO 8601 compliant, our task was quite easy). We had to remove all the occurrences of:

<?php

$var = date("W");

?>

and replace them with:

<?php

$var = strftime("%V");

?>

since the result of strftime('%V') is ISO 8601 compliant.