Smith and Wooten - Improv Google Is Not Immune To Problems

PHP Speed Hack - Date Function

Here is a little tip that has helped me in the past to optimize a site under load. I like to access time() and date() as little as possible because it is a call that can be used easily but has some overhead when it is called a lot. Below for the $start and $end calls I could call it 5 times, but doing it this way means I only call it once.

$tmp = date("d-m-Y-t", time()); //or whatever string formats you need
$date = explode("-",$tmp);

Now you can use the information in the $date array. I use this lots when doing calls to mktime() etc where I need to calculate start/end times for search results etc:

$start = mktime(0,0,0,$date[1],1,$date[2]);
$end = mktime(23,59,59,$date[1],$date[3],$date[2]);

This will give you a little decrease in the overall load of php on your server :)


About this entry