php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45409 strtotime() very slow when operating on timestamp
Submitted: 2008-07-01 20:55 UTC Modified: 2009-05-03 18:51 UTC
Votes:10
Avg. Score:5.0 ± 0.0
Reproduced:10 of 10 (100.0%)
Same Version:9 (90.0%)
Same OS:4 (40.0%)
From: jerico dot dev at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.*, 6CVS (2008-12-09) OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jerico dot dev at gmail dot com
New email:
PHP Version: OS:

 

 [2008-07-01 20:55 UTC] jerico dot dev at gmail dot com
Description:
------------
strtotime() performs 3 orders of magnitude slower when operating on a timestamp.

Reproduce code:
---------------
$string = '1902-03-25 18:25:25';
$timestamp = strtotime($string); 

$mt0 = microtime(true);
$result1 = strtotime("$string +1 year");
$mt1 = microtime(true);
$result2 = strtotime("+1 year", $timestamp);
$mt2 = microtime(true);
printf("%.6f\r\n%.6f\r\n", $mt1-$mt0, $mt2-$mt1);

// This will output
// 0.000171
// 0.721877
// on my machine

Actual result:
--------------
see reproduce code

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-01 21:30 UTC] derick@php.net
I'll have a look at this, but there is even a MUCH faster way:

<?php
$d = new DateTime( '1902-03-25 18:25:25' );
$mt0 = microtime(true);

$d->modify( '+1 year' );
$mt1 = microtime(true);
$d->modify( '+1 year' );
$mt2 = microtime(true);
printf("%.6f\r\n%.6f\r\n", $mt1-$mt0, $mt2-$mt1);
?>
0.013312/0.696215 for your code, vs 0.000065/0.000028 for my approach.
 [2009-02-04 13:47 UTC] php at wallbash dot com
Hi,

i hope i don't bug you but do you know when this might get fixed or is there a patch available ? We (finally) moved to 5.2 and this is a showstopper for us and would (sadly) require lots of refactoring.

Thanks
 [2009-05-03 18:51 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

This seems to be a duplicate of bug #48522, which is now fixed in CVS. 
I now get the following result with your original code.

0.000047
0.000079

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 13:01:33 2025 UTC