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: 2018-05-05 21:39 UTC
From: viewport2heaven at yahoo dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.8 OS: Windows 7
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: 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

Pull Requests

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...
 [2018-05-05 21:39 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-05 21:39 UTC] requinix@php.net
Old bug report is old.

As commented, the plus sign belongs to the 3 days and not to the entire string.

http://php.net/manual/en/datetime.formats.relative.php
> number space? (unit | 'week')
> Handles relative time items where the value is a number.

-3 days is one amount, 2 hours is another, 3 minutes another, and 4 seconds another.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 07:01:31 2025 UTC