Hello, tell me, is there any smart function in PHP that would accept the string " 29 dec 2016 " as the base date and determine that the string " 18 jan " is already 2017?

This does not work as I need:

$date1 = strtotime('29 dec 2016'); $date2 = strtotime('18 jan', $date1); echo date("Ymd H:i:s", $date2); // 2016-01-18 00:00:00 
  • Why not write it yourself? If the month of the next date is less than the previous one, it means another year. Roughly speaking ..... In fact, this does not mean that another year or a year is more than the previous one .... but if you have a problem to add on a year, then ... - Alexey Shimansky
  • If "31 dec"? Maybe 2017 is meant? Or maybe "18 jan" - 2020? Any restrictions? - vikttur
  • It is possible to write something, but it is necessary to write and test, but what if there is a solution already tested. No, there is strictly a line with any current date, for example, December 31, 2016 and there is a line for example 1 jan and 100% it’s clear that this is 1 jan this is 2017 - iormark
  • And what to test? the solution is simple and reinforced - Artem Gorlachev
  • но это же надо писать .... really. to think more, the horror is the same)) - Alexey Shimansky

1 answer 1

Just check the current date of January 18 and add the year.

 $date1 = strtotime('29 dec 2016'); $date2 = strtotime('18 jan', $date1); if ($date1 > $date2) { $date2 = strtotime('+1 year', $date2); } echo date("Ymd H:i:s", $date2); // 2017-01-18 00:00:00 
  • Yes, it will work, 2016 will remain. Corrected a comparison of dates, compared with the current - Artem Gorlachev
  • So did before, but there was some kind of trick and I can not remember which one. Thanks, I’ll be using it for now) - iormark