I need to define the start and end dates of a tax year, but I don't seem to find anything with relative formats which gives me something like this:
strtotime('6th day of last april')
(doesn't work)
The thing is that if today is for example the 10th of May, then the year is the same; however, if we are in February, the year must be the past one.
I assume there is a better way to determine the year rather than by using if statements? But I can't find this case in the dates related documentation.
EDIT: According to this answer, there seems to be no direct syntax for it and some code is required.
CodePudding user response:
If the year is missing in a date expression, Datetime takes the current year for the year. However, the date you want to create contains a condition. If the created date is in the future, 1 year should be deducted. This cannot be solved with the relative DateTime formats. As an alternative to the if construct, I can only offer the ternary operator:
$date = ($date = date_create('Apr 06')) < date_create('Today')
? $date
: $date->modify('-1 Year')
;
CodePudding user response:
A posible solution would be:
$date=strtotime('Apr 06');
if($date > time()) $date = strtotime("-1 year", $date);