搜尋
首頁後端開發php教程strtotime函数一个很奇怪的问题

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

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


回复讨论(解决方案)

我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;    }}


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();}



我在使用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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP電子郵件:分步發送指南PHP電子郵件:分步發送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

如何通過PHP發送電子郵件:示例和代碼如何通過PHP發送電子郵件:示例和代碼May 09, 2025 am 12:13 AM

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

高級PHP電子郵件:自定義標題和功能高級PHP電子郵件:自定義標題和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送電子郵件的指南使用PHP和SMTP發送電子郵件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

使用PHP發送電子郵件的最佳方法是什麼?使用PHP發送電子郵件的最佳方法是什麼?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依賴注入的最佳實踐PHP中依賴注入的最佳實踐May 08, 2025 am 12:21 AM

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

PHP性能調整技巧和技巧PHP性能調整技巧和技巧May 08, 2025 am 12:20 AM

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

PHP電子郵件安全性:發送電子郵件的最佳實踐PHP電子郵件安全性:發送電子郵件的最佳實踐May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)