Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

datetime - 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

strtotime_edate:- 1610389800

dateset:-

question from:https://stackoverflow.com/questions/66047634/strtotime-in-php-returning-blank-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To paraphrase @iainn: I'm not 100% sure why you're changing back and forth between DateTime objects and function calls to 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 (d-m-Y)? Hopefully it is, in which case PHPs strtotime function understands it correctly and produces a Unix timestamp (i.e. seconds passed since start of 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...

$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...

  1. In the format: HisYmd

  2. But your input is too short for that so it only matches HisY

    Time => 16:10
    Year => 9600
    
  3. Given the lack of data DateTime then fills in the blanks with today (example: 2021-02-05)

    Day   => 05
    Month => 02
    
  4. Which give you a complete timestamp of: 9600-02-05 16:10:40

strtotime from DateTime

Your next line of code then passes that timestamp back into a strtotime call...

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. Which means the first problem is that you're using echo which doesn't 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?

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.

To fix this specific problem I suggest you update your PHP version (and OS if you haven't moved to 64 bit!)

However, further to that, I strongly suggest you stick to the DateTime object/class. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...