Home > Software design >  Subtracting days from a future date
Subtracting days from a future date

Time:08-03

$tour_date = '11 December 2022';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date))); 
echo 'Make payment until $newdate';

It is working as expected. $newdate prints as 1 December 2022

However, if the $tour_date is not the current year, it does not work properly. It is still printing as 1 December 2022.

$tour_date = '11 December 2023';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date))); 
echo 'Make payment until $newdate';

The $newdate prints as 1 December 2022. But it should be 1 December 2023 which is -10 days from 11 December 2023.

Any idea, that will work with the current year and also future dates?

Edit:

My bad that I did not mention the date is actually like this: $tour_date = '11 December, 2023';

Actually, the app Grammarly removed the comma when I submit the question.

Luckily, @Rylee read my comments and found the problem. Thank you very much!

CodePudding user response:

You mentioned in a comment that your date is stored as 11 December, 2023.

The comma , is preventing PHP from parsing the date string correctly. Remove the comma (using str_replace or similar) and try again.

echo date("Y-m-d", strtotime("-10 day", strtotime("11 December 2023")));  // 2023-12-01
echo date("Y-m-d", strtotime("-10 day", strtotime("11 December, 2023"))); // 2022-12-01

In the case with the comma , it can't determine the year correctly so it defaults to this year.

After more testing - there seems to be some weird results with that format. I'm not sure why this is exactly:

// Using date("Y-m-d", strtotime($input));
11 December, 1970 -> 1970-12-11
11 December, 1999 -> 1999-12-11
11 December, 2000 -> 2022-12-11
11 December, 2001 -> 2022-12-11
11 December, 2020 -> 2022-12-11
11 December, 2059 -> 2022-12-11
11 December, 2060 -> 2060-12-11
11 December, 2099 -> 2099-12-11

It seems that the "year" values after the comma have a special case between 2000 and 2059 (inclusive).

CodePudding user response:

echo 'Make payment until $tour_date (1 December 2022)';

Surely it should echo $newdate?

CodePudding user response:

This:

$tour_date = '11 December 2023';
$newdate = date(
    'j F, Y',
    strtotime(
        '-10 days',
        strtotime($tour_date)
    )
); 
echo "Make payment until $newdate";

outputs:

Make payment until 1 December, 2023

Notice that I replaced '-10 day' with '-10 days'. And I used " instead of ' when I used echo. Nevertheless, using '-10 day' also worked for me… I suspect you made a mistake when you copied the code here. Originally, your code here had:

echo 'Make payment until $tour_date (1 December 2022)';

and it was replaced with:

echo 'Make payment until $newdate';

You could also have done something like this:

$tour_date = '11 December 2023';
$newdate = date('j F, Y', strtotime("-10 days $tour_date")); 
echo "Make payment until $newdate";

Or:

$newdate = (new DateTimeImmutable('11 December 2023'))
    ->sub(new DateInterval('P10D'))
    ->format('j F, Y');
echo "Make payment until $newdate";

CodePudding user response:

The code works as expected. Try changing the year on $tour_date string

$tour_date = '11 December 2022';

CodePudding user response:

PHP is cursed and nobody should be using it, in my opinion at least. Either way, here's a working example of that which you intended to achieve:

<?php
$originalDate = "2023-12-11";
$daysToSubtract = 10;
$date=date_create($originalDate);
$subtracteddate = date_sub($date,date_interval_create_from_date_string($daysToSubtract . " days")); ###Could be " months", or " seconds"
echo "Original Date: " . date_format($date,"Y-m-d") . "\r\r";
echo "Original minus " . $daysToSubtract . " Days: " . date_format($subtracteddate,"Y-m-d");
?>
  • Related