-2

I get stupid datetimes such as this from the outside:

Jan-02 19:00

That means "the next 2nd January, 19 o'clock".

Currently, since the current time is in the very end of 2021, that means 2022-01-02 19:00. But if I sen that to the datetime parser in PHP, it will think I'm talking about the current year's "Jan-02", namely 2021-01-02 19:00. That's in the past.

I would never give a year-less datetime myself, but I don't control this. It's also impossible to contact them and have them change anything.

This is probably extremely simple, but it's caused me grief for years when the date formatter misinterprets the date as referring to the past, when it always means "the next time this date occurs from the current time".

  • 3
    Are all dates supposed to be in the future? Convert the input to a timestamp. Check if it's less than `time()`. If it is, add a year to it. – Barmar Dec 30 '21 at 20:44
  • The datetime parser supports lots of relative phrases like `next monday` or `first day of jan next year`, you might be able to find one that works for your case. (Sadly `next Jan 5` doesn't seem to be one of them.) – Alex Howansky Dec 30 '21 at 20:55
  • If the format is stable then you could prepend that string with the current year , convert to DateTime, check if its in the future and ifnot then repeat the process but for next year. – Raxi Dec 30 '21 at 21:12
  • 1
    theoretically you could also simply just parse it as is, and then override the year with current or next year, there is a function for that; but the edge cases will drive you nuts (eg: not all dates nor times exist in every year, stuff like that) – Raxi Dec 30 '21 at 21:14

1 Answers1

0

I don't know the exact rules you may need to go by, but seeing a date like that I would use basic rule that if the date provided is less than the current date, set it to the next year. Feel free to use your own rule if that is not how the input date works.

Also keep in mind the timezone. There is no timezone in the datetime example you provided. It may be different than the server default which could be any timezone or UTC - so you may end up with a different time/date than you expect.

$inputDT = 'Jan-02 19:00';

// Keep timezones in mind!
// The input date has no timezone defined and may differ
//   from server or server may be set to UTC.
$dt = DateTime::createFromFormat('Y-M-d H:i', date('Y') . '-' .$inputDT);

// If the input date is less than the current date,
//    add 1 to the year.
if($dt < (new DateTime())) {
    $dt = DateTime::createFromFormat('Y-M-d H:i', (date('Y') + 1) . '-' . $inputDT);
    // Assuming current date is in December 2021,
    //   $dt is now January 2, 2022 @ 19:00
}
Jim
  • 3,210
  • 2
  • 17
  • 23
  • @yivi I just tested it and it is correct behavior. If it were Feb 29 2024 it is accepted because it is a valid date, but for this year or any other non-leap year that is an invalid date so it corrects to Mar 1. You'll need to add additional logic if you want it to work the way you want it to. – Jim Jan 11 '22 at 12:22
  • 1
    @yivi Updated the answer to account for leap year. – Jim Jan 11 '22 at 12:47