php / php/php-src

strtotime() has strange interpretation of decimal weeks

未关闭
#21,027 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@derickr 已经在做这个了。

开始于 2026年1月24日。

Extension: date Feature Status: Requires RFC
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

The following code:

<?php
strtotime("2.37685 weeks");

Resulted in this output:

1613443020

The code interpreted 2.37685 weeks as 02:37 (hours) and 685 weeks. This is non logical input of course (and not GNU compliant). But this isn't a very logical interpretation of this value from a human perspective and can easily be used incorrectly by users of the code without noticing.

The parser decided to not be able to recognize weeks, then fallback to hours, interpret up to 2 decimals (compliant with hour parsing), then split the remainder and continue parsing, interpreting the rest as the input for weeks.

This has been a known (usability) issue since 2007, when it was first encountered by Wikipedia, but an upstream ticket was never filed until now.

I consider this a usability pitfall of this function, that in my opinion should be rectified

  • it is illogical that a decimal is cutoff at 2 places
  • the cutoff value is assigned an implicit unit
  • the remainder of the value is suddenly a NEW secondary input
  • it adds both of them to form something way bigger than the user had expected

I suggest that if a decimal is recognized and unit is weeks, that this function throws a warning (recognize but don't accept)

PHP Version
PHP 8.4.16 (cli) (built: Dec 16 2025 16:03:34) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.4.16, Copyright (c) Zend Technologies
    with Xdebug v3.4.0, Copyright (c) 2002-2024, by Derick Rethans
    with Zend OPcache v8.4.16, Copyright (c), by Zend Technologies
Operating System

macos

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。