Bug #60550 [Opn->Nab]: strtotime adds compound string, subtracts wrong

From: Date: Sat, 05 May 2018 21:39:27 +0000
Subject: Bug #60550 [Opn->Nab]: strtotime adds compound string, subtracts wrong
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215103@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60550&edit=1 ID: 60550 Updated by: requinix@php.net Reported by: viewport2heaven at yahoo dot com Summary: strtotime adds compound string, subtracts wrong -Status: Open +Status: Not a bug Type: Bug Package: Date/time related Operating System: Windows 7 PHP Version: 5.3.8 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2011-12-21 17:49:16] 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... ------------------------------------------------------------------------ [2011-12-17 04:02:03] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=60550&edit=1

« previous php.bugs (#215103) next »