1

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:

Adding days to $Date in PHP

PHP date time greater than today

Subtract 1 day with PHP

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

Raül
  • 137
  • 3
  • 15

2 Answers2

1

Depending on the magic of PHP like with date_create might behave different than expected. https://secure.php.net/manual/en/function.date-create.php - read the second comment. Since you are using the dd/mm/YYYY format this might not work correct.

In the second example you are using.

$date = $item->getData('attribute_name'); //Value as String
$userDate = DateTime::createFromFormat('d/m/Y', $date);`

But first line of your question starts with :

'd-m-y'. Example -> '20-02-2019', as a String.

Taking that is your input your createFromFormat is not 'd/m/Y' according to the example date you gave (it is d-m-Y)

slayergirl
  • 29
  • 3
  • My bad, it was wrongly written in the question, I just fix it. I tried putting dd/mm/yyyy but that doesn't fix it either, the result gives me bool(false). – Raül Feb 20 '19 at 10:34
  • Which result gives false? Using the second example you gave I do get a valid DateTime object with input Example -> '20/02/2019' Do not use the date_create in this case it is unreliable. Creating it from a format gives you more control over what is happening so would always be my choice. – slayergirl Feb 20 '19 at 14:59
1

Raül.

If you want to add days to a date, you should do it on the string and then convert it to DateTime:

If I got it right, you have 3 dates:

  • The one that comes from $item->getData('attribute_name'); as String
  • The user date from $userDate = DateTime::createFromFormat('d/m/Y', $date); as DateTime
  • The "+ 15 days" date, which you have to calculate.

Before converting your string to DateTime object (in $data = date_create(date('d/m/Y',strtotime($date)));), you should calculate your "+ 15 days" date.

Here's how I would do it:

    $datestring = '05/02/2019';

    $date = date_create(date('d/m/Y',strtotime($datestring))); //first date

    $userDate = DateTime::createFromFormat('d/m/Y', '22/02/2019'); //user date

    if ($userDate === false) {
        throw new InvalidArgumentException('Invalid date string');
    }


    $cmp = DateTime::createFromFormat('d/m/Y',date('d/m/Y', strtotime($date->format('Y/m/d').' + 15 days'))); //first date +15 days


    if ($userDate <= $cmp && $userDate >= $date) {
       $result = "The user date is in range";
    }
    else {
        $result = "The user date is out of range";
    }

    echo('<b>First date: </b>'.$date->format('d/m/Y'));
    echo('<br> <b>User date : </b>'.$userDate->format('d/m/Y'));
    echo('<br> <b>Date + 15 : </b>'.$cmp->format('d/m/Y'));


    echo('<br><br>'.$result);

Also, the ' + 15 days' concatenation only worked for me in Y/m/d format. That's why I formatted it to calculate.

In my example I compare the $userDate to check if it falls between the other two, but you can compare them as you wish.

Hope it helps

francovici
  • 536
  • 6
  • 14