According to the gmmktime() description in the PHP manual, it uses mktime() internally. Yet when I run the following code, the mktime loop takes just under 9 seconds to run while the gmmktime look takes just under 2 seconds. How can this be?
<?php
$count = 1000000;
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++)
{
mktime();
}
$endTime = microtime(true);
printf("mktime: %.4f seconds\n", $endTime - $startTime);
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++)
{
gmmktime();
}
$endTime = microtime(true);
printf("gmmktime: %.4f seconds\n", $endTime - $startTime);
Output:
mktime: 8.6714 seconds
gmmktime: 1.6906 seconds
Most likely, the documentation is lying to you about how
gmmktime()is implemented – or it means the C functionmktime()is being used.If we look at the actual code, both
gmmktime()andmktime()pass through to an internalphp_mktimefunction, which takes agmtparameter (set to1forgmmktime()). Ifgmtis zero, then it has to do some extra work (//-comments I have added, others from original code):I suspect what you may find is that, every time you do
mktime(), it reopens the timezone description file to read it and get the proper timezone/DST offsets. By usinggmmktime(), it skips that by using an internal null timezone for GMT – thus, much faster.