Hi
I need to to display 2 dates. A ‘from’ date and a ‘to’ date (which should be a year from the ‘from’ date minus 1 day).
Ive got the from date working ($form_data[‘date_created’] is in the format dd/mm/yyyy):
If you look further at strtotime() you can specify modifiers such as “+ 1 year” and “- 1 day” to help do the heavy lifting. I don’t know personally if they deal with leap years, I imagine they do.
I’m concerned about ‘Yesterday’ if the date is the 1st of March, 2019. Because then ‘yesterday’ is Feb 28th, +1 year = 28th Feb 2020. But “1 year from today minus a day” would be the 29th…so order of operations would matter.
You are quite right.
It seemed logical to do it in that order to get it in one shot. Ordinarily with addition and subtraction, order of operation isn’t a concern, but in this case it is.
#1: Why are we doing milliseconds? #2: Except this year, there are 366 * 24 * 60 * 60 * 1000 (milli)seconds. Your math loses a day every 4 years (except years divisible by 100, but not excepting years divisible by 400…)
I can’t see why you’d need such potential for trouble. Doing it in milliseconds won’t help make it easier to decide whether you need to worry about leap years, all it’ll do is add anxiety about “leap seconds”.
That code doesn’t take into account leap years, so it’s a step backwards really. I think if I wanted to do it by number manipulation I’d probably just do something like:
year += 1
day-of-month -=1
if day-of-month < 1
month -=1
if month < 1 month = 12
day-of-month = last valid day of month
ifend
Does it give the correct answer for all your test cases, with and without leap years, starting on the first day of a month, starting on the last day of a month, starting on New Years day, and load of others?
I think to the nearest day is the level of accuaracy you need to worry about.
So sticking with my simplistic approach, with @m_hutley’s correction, I’m throwing in:-
$toDate = date('F j, Y', strtotime("next year - 1 day"));