Determining PHP script execution time

php-logo

Today I needed to quickly determine how long a script was taking to execute. Whilst a dedicated class could offer a lot more (including milestones) I needed something to do a quick ‘sanity check’ on a script. Heres how I did it:

function determinetime_float()
{
list($utime, $time) = explode(” “, microtime());
return ((float)$utime + (float)$time);
}

$script_start = determinetime_float();

Put the above at the top of your page, and put the below at the bottom of your page:

$script_end = determinetime_float();
echo “PHP script executed in “.bcsub($script_end, $script_start, 4).” seconds.”;

So, – whats happening?
PHP’s microtime() function gives the current timestamp in microseconds (we need this level of precision for script execution time). However microtime() returns a value like 796374521.15534500, when what we really want is 0.15534500 796374521 (The 0 is removed from the decimal section and added on to the integer). We use the above function to take care of this.

PHP5 date() Function

php-logo

Whilst migrating a clients website to a new server today I came across an unexpected error:

Strict Standards: date() function.date: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. (etc)

Well, if you are on shared hosting you most likely do not have access to the php.ini file in order to set the timezone.
Instead head along to your .htaccess file and add the folllowing line:
php_value date.timezone [your_zone] where [your_zone] is found from the link here

PHP 5 Mailer Class

phpmailer

I spent about a day creating a basic mailer class until I decided that there had to be a much better implementation created by a third party. Whilst it was interesting to have to iron out the bugs in the examples I found on the internet ultimately I wanted a solid, secure platform for both myself and my customers. There are a lot of code snippets on the internet that just are not up to the job…

So, the one to use is PHPMailer from Codeworx Technologies. Find their link here and access the script from their SourceForge page

For reference my functional example is located here
Let me know if you require a more detailed explanation of how it works.

Update: this page has been getting a few views, so it is obviously something that people are interested in. As of this edit (20/05/2010) the version is 5.0.0.0

Features include:

  • Supports emails digitally signed with S/MIME encryption!
  • Supports emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Works on any platform.
  • Supports Text & HTML emails.
  • Embedded image support.
  • Multipart/alternative emails for mail clients that do not read HTML email.
  • Flexible debugging.
  • Custom mail headers.
  • Redundant SMTP servers.
  • Support for 8bit, base64, binary, and quoted-printable encoding.
  • Word wrap.
  • Multiple fs, string, and binary attachments (those from database, string, etc).
  • SMTP authentication.
  • Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.Good documentation, many examples included in download.
  • It’s swift, small, and simple.