Web & Software Developer

PHP strftime() is Faster Than date() in PHP 7

After running several benchmark tests, I find that the PHP strftime() is faster than date(). I tested each one creating the same datetime string in this format: ‘YYYY-MM-DD HH:MM’.

The test iterates 100000 times over each function. I repeated this test several times. Each time, I alternated which function ran first. Here are the benchmark speed test results. The average execution times are for running 100000 times, and they are in milliseconds.

PHP ‘strftime()’ versus ‘date()’

Average execution time for strftime(): 266.6 ms
Average execution time for my_date(): 293.8 ms

 

This was using PHP version 7.0.27. To run this benchmark test, I used my benchmarking template and the following functions:

function my_strftime(){
    $mktime = mktime(2,0,0,03,27,1921);
    $datetime = strftime("%Y-%m-%d %H:%M", $mktime);
}
function my_date(){
    $mktime = mktime(2,0,0,03,27,1921);
    $datetime = date('Y-m-d H:i', $mktime);
}

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]