Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial strtotime函数一个很奇怪的问题

strtotime函数一个很奇怪的问题

Jun 20, 2016 pm 12:49 PM

var_dump(strtotime('1441185010'));//输出bool(false)var_dump(strtotime('1451382400'));//输出int(13591003898)
Copy after login

很奇怪,为什么这两个的结果不一样呢?


回复讨论(解决方案)

我PHP5.6返回两个false,你的PHP是什么版本。

我PHP5.6返回两个false,你的PHP是什么版本。


我是PHP Version 5.3.3,centos6.4 64位虚拟机,第一个正常输出false,第二个就不对了。
实在奇怪到底是什么问题引起的

不知道你要用来干嘛?
strtotime是将任何英文文本的日期时间描述解析为 Unix 时间戳
echo strtotime('2015/12/11');
如果你要把时间戳转换为日期,用date啊

不知道你要用来干嘛?
strtotime是将任何英文文本的日期时间描述解析为 Unix 时间戳
echo strtotime('2015/12/11');
如果你要把时间戳转换为日期,用date啊



modifier.date_format.php
function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null){    if (substr(PHP_OS,0,3) == 'WIN') {           $_win_from = array ('%e',  '%T',       '%D');           $_win_to   = array ('%#d', '%H:%M:%S', '%m/%d/%y');           $format = str_replace($_win_from, $_win_to, $format);    }    if($string != '') {        return strftime($format, smarty_make_timestamp($string));    } elseif (isset($default_date) && $default_date != '') {        return strftime($format, smarty_make_timestamp($default_date));    } else {        return;    }}
Copy after login


shared.make_timestamp.php
function smarty_make_timestamp($string){    if(empty($string)) {        $string = "now";    }/** 这个判断是我做的一个临时救急解决方案,并没有找到实际原因。    if(strlen((int)$string)==10){        return (int)$string;    }*/    $time = strtotime($string);//此处在处理时间戳的时候发现问题里第一个时间戳正常返回false,但是第二个却返回一个不知道怎么得出来的整型,结果与$string不一致,结果自然不对    if (is_numeric($time) && $time != -1)        return $time;    // is mysql timestamp format of YYYYMMDDHHMMSS?    if (preg_match('/^\d{14}$/', $string)) {        $time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),               substr($string,4,2),substr($string,6,2),substr($string,0,4));        return $time;    }    // couldn't recognize it, try to return a time    $time = (int) $string;    if ($time > 0)        return $time;    else        return time();}
Copy after login



我在使用smarty模板格式化时间的时候,使用这种{$aList[list].inputtime|date_format:"%Y-%m-%d %H:%M:%S"},但是这个时间出现错误,在进一步跟踪错误的过程中发现是smarty是直接把时间戳当做字符串交个一个函数处理,这个函数smarty_make_timestamp内部使用strtotime直接处理该字符串,根据处理结果返回不同值。结果却是部分正确,部分错误,貌似时间分界点是以2015-9-4号,所以很奇怪。

echo date('Y-m-d H:i:s', 1441185010); //2015-09-02 17:10:10
可知 1441185010 是有效的时间戳

strtotime -- 将任何英文文本的日期时间描述解析为 Unix 时间戳
如果你 $time = strtotime($string); 
而 $string 不是 英文文本的日期时间描述 的话,那自然是错误的了
echo date('Y-m-d H:i:s', strtotime('20150902171010')); //2015-09-02 17:10:10
echo date('Y-m-d H:i:s', strtotime('20150902')); //2015-09-02 00:00:00
你的 1441185010 是否能解释成 1441 年 18 月 50 日 10 时 ????

echo date('Y-m-d H:i:s', 1441185010); //2015-09-02 17:10:10
可知 1441185010 是有效的时间戳

strtotime -- 将任何英文文本的日期时间描述解析为 Unix 时间戳
如果你 $time = strtotime($string); 
而 $string 不是 英文文本的日期时间描述 的话,那自然是错误的了
echo date('Y-m-d H:i:s', strtotime('20150902171010')); //2015-09-02 17:10:10
echo date('Y-m-d H:i:s', strtotime('20150902')); //2015-09-02 00:00:00
你的 1441185010 是否能解释成 1441 年 18 月 50 日 10 时 ????



echo date('Y-m-d H:i:s', 1451382400);
//输出2015-12-29 17:46:40
echo date('Y-m-d H:i:s', strtotime('1451382400'));
//输出2400-09-06 14:51:38

echo date('Y-m-d H:i:s', 1441185010); //2015-09-02 17:10:10
可知 1441185010 是有效的时间戳

strtotime -- 将任何英文文本的日期时间描述解析为 Unix 时间戳
如果你 $time = strtotime($string); 
而 $string 不是 英文文本的日期时间描述 的话,那自然是错误的了
echo date('Y-m-d H:i:s', strtotime('20150902171010')); //2015-09-02 17:10:10
echo date('Y-m-d H:i:s', strtotime('20150902')); //2015-09-02 00:00:00
你的 1441185010 是否能解释成 1441 年 18 月 50 日 10 时 ????




我的情况跟直接输出不太一样,我这的情况是根据strtotime的结果来判断是直接返回时间戳还是返回经过strtotime处理过的时间,正常strtotime处理时间戳是返回false的,但是我的情况是部分返回false,比如1441185010,但是1451382400返回的就不是false了。

由于你传递给 strtotime 的是是时间戳,所以即便不是 false 也是不对的
echo date('Y-m-d H:i:s', 13591003898); //1992-05-17 19:26:50

由于你传递给 strtotime 的是是时间戳,所以即便不是 false 也是不对的
echo date('Y-m-d H:i:s', 13591003898); //1992-05-17 19:26:50


好吧,版主,先谢谢你。
我之前说过哦,我的情况并不是直接输出的,而是根据strtotime返回的结果在用date输出。

我现在的问题是为什么
strtotime('1441185010')
是false;

strtotime('1451382400')
却不是false,理论上讲strtotime(10位时间戳)不应该输出false的吗?

那是你的 php 有问题
经测试都是 false

那是你的 php 有问题
经测试都是 false



经测试确实是php版本的问题,5.3的版本出现上述问题,5.4的版本都是false,只是不知这到底是原因?

5.2的版本同样有上述问题,不清楚这到底是bug还是什么

楼主你去看看这个帖子。。。。http://bbs.csdn.net/topics/391822092

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

See all articles