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...
In the format: HisYmd
But your input is too short for that so it only matches HisY
Time => 16:10
Year => 9600
Given the lack of data DateTime
then fills in the blanks with today
(example: 2021-02-05)
Day => 05
Month => 02
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