We basically have a date format like: 'd/m/y'. Example -> '20/02/2019', as a String.
We need to be able to get the date of today and the date of today + 15 days. And then check if the date given at the start is between these days or not.
Date X given. Check if Date X is bigger than today's Date A and +15 days Date B.
We would like to get all the values with the type DateTime and with that be able to compare if the date given is between them.
We have checked several different questions but I can't manage to get it done, in my opinion because as far as I have seen, i'm not using the php function strtotime properly.
We have checked all this questions:
PHP date time greater than today
A non well formed numeric value encountered
PHP date compare older than 15 days
And other pages, very useful:
https://benholland.me/tutorials/2012/03/11/adding-and-subtracting-dates-and-times-in-php.html
But still, the values are wrong.
$date = $item->getData('attribute_name'); //Value as String
$data = date_create(date('d/m/Y',strtotime($date))); //Creating the DateTime object.
But this automatically gives us:
object(DateTime)#5793 (3) { ["date"]=> string(26) "1970-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Madrid" }
In my opinion it must be some kind of formatting mistake which automatically gives me that date, always.
I have also tried as the last question:
$date = $item->getData('attribute_name'); //Value as String
$userDate = DateTime::createFromFormat('d/m/Y', $date);
if ($userDate === false) {
throw new InvalidArgumentException('Invalid date string');
}
$cmp = new DateTime('+15 days'); //today's date +15
if ($userDate <= $cmp) {
$userDate = new DateTime();
}
echo date_format($userDate, 'd/m/Y');
What am I missing? Is there an improved way to do it?
Thanks