简体   繁体   English

php 中的 STRTOTIME 返回空白值

[英]STRTOTIME in php returning blank value

The dataset value is returning blank, no error on logfile.数据集值返回空白,日志文件没有错误。

$edate = trim($_POST['txtedate']); //user inputs date 12-01-2021
$int_effective_date = new DateTime(strtotime($edate));
echo "edate:- ".$edate."<br />";        
echo "strtotime_edate:- ".strtotime($edate)."<br />";
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));

Result:结果:

edate:- 2021-01-12日期:- 2021-01-12

strtotime_edate:- 1610389800 strtotime_edate:- 1610389800

dateset:-日期:-

To paraphrase @iainn: I'm not 100% sure why you're changing back and forth between DateTime objects and function calls to strtotime ?套用@iainn:我不是100%确定您为什么要在DateTime对象和 function 调用strtotime之间来回切换?

However, I can explain the most likely issue with your code...但是,我可以解释您的代码最有可能出现的问题......

strtotime时间

Firstly, let's clarify that 12-01-2021 is in the format ( dmY )?首先,让我们澄清一下12-01-2021的格式是( dmY )吗? Hopefully it is, in which case PHPs strtotime function understands it correctly and produces a Unix timestamp (ie seconds passed since start of 1970)...希望是这样,在这种情况下,PHP strtotime function 可以正确理解它并生成 Unix 时间戳(即自 1970 年开始以来经过的秒数)...

strtotime("12-01-2021");

// Output: 1610409600

// Notes:
//     - Possible slight variations based on locale etc.
//     - Lookup: date_default_timezone_set
//     - This is with "UTC"

DateTime约会时间

You then pass that timestamp to DateTime but neglect to inform DateTime what kind of timestamp it is...然后,您将该时间戳传递给DateTime但忽略通知DateTime它是哪种时间戳......

$int_effective_date = new DateTime(strtotime($edate));

// Is the same as...

$int_effective_date = new DateTime(1610409600);

However, DateTime doesn't see your timestamp as incorrect and tries to process it anyway...但是, DateTime不会认为您的时间戳incorrect ,并且无论如何都会尝试处理它......

  1. In the format: HisYmd格式: HisYmd

  2. But your input is too short for that so it only matches HisY但是你的输入太短了,所以它只匹配HisY

     Time => 16:10 Year => 9600
  3. Given the lack of data DateTime then fills in the blanks with today (example: 2021-02-05)鉴于缺少数据DateTime然后用today填空(例如:2021-02-05)

     Day => 05 Month => 02
  4. Which give you a complete timestamp of: 9600-02-05 16:10:40这给你一个完整的时间戳: 9600-02-05 16:10:40

strtotime from DateTime来自日期时间的 strtotime

Your next line of code then passes that timestamp back into a strtotime call...然后,您的下一行代码将该时间戳传递回strtotime调用...

echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));

// Is the same as...

echo "dateset:- ".strtotime("9600/02/05");

Now, strtotime will always return something .现在, strtotime总是会返回一些东西 Which means the first problem is that you're using echo which doesn't output (bool) false .这意味着第一个问题是您使用的echo不是 output (bool) false

Try:尝试:

var_dump(strtotime("9600/02/05"));

You might ask, why doesn't that happen in the linked code example from @El_Vanja?您可能会问,为什么 @El_Vanja 的链接代码示例中没有发生这种情况?

Answer回答

The answer to that, I believe, is that your PHP version is not up to date and anything over the 32 bit date range is going to return (bool) false from strtotime .我相信,答案是您的 PHP 版本不是最新的,超过32 bit日期范围的任何内容都会从strtotime返回(bool) false

To fix this specific problem I suggest you update your PHP version (and OS if you haven't moved to 64 bit!)要解决此特定问题,我建议您更新您的 PHP 版本(如果您还没有移至 64 位,则更新操作系统!)

However, further to that, I strongly suggest you stick to the DateTime object/class.但是,除此之外,我强烈建议您坚持使用DateTime对象/类。 It saves you from all of these annoying bugs if nothing else...如果没有别的,它可以让您摆脱所有这些烦人的错误......

For reference:以供参考:

echo strtotime( (new DateTime("@1610409600"))->format("Y-m-d") ); // Output: 1610409600
echo strtotime( (new DateTime("2021-01-12"))->format("Y-m-d") );  // Output: 1610409600

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM