php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60550 strtotime adds compound string, subtracts wrong
Submitted: 2011-12-17 04:02 UTC Modified: -
From: viewport2heaven at yahoo dot com Assigned:
Status: Open Package: Date/time related
PHP Version: 5.3.8 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: viewport2heaven at yahoo dot com
New email:
PHP Version: OS:

 

 [2011-12-17 04:02 UTC] viewport2heaven at yahoo dot com
Description:
------------
---
From manual page: http://www.php.net/function.strtotime#refsect1-function.strtotime-parameters
---

A string of '+1 day 2 hours 3 minutes 4 seconds' works.
A string of '-1 day 2 hours 3 minutes 4 seconds' doesn't work.

Test script:
---------------
    $time = strtotime('+3 days 2 hours 3 minutes 4 seconds') - time();
    $expectedDiff =
      3 * 24 * 60 * 60 +
      2 * 60 * 60 +
      3 * 60 +
      4;

    $time = strtotime('-3 days 2 hours 3 minutes 4 seconds') - time();
    $expectedDiff =
      3 * 24 * 60 * 60 +
      2 * 60 * 60 +
      3 * 60 +
      4;

Expected result:
----------------
+ and - should be the same.

Actual result:
--------------
+ is correct. - is wrong.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-21 17:49 UTC] jayte dot boehler at bestnotes dot com
The manual doesn't clarify how php processes a sequential set of number-based relative values, but it appears that the initial '+/-' does not dictate the direction of the entire sequence.  If you continue in the negative direction with the string '-3 days -2 hours -3 minutes -4 seconds', it yields the correct diff...
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC