简体   繁体   中英

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

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 ( 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)...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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