0

I use DateTime::creaeFromFormat to create datetime from week number. But it always return false.

$date = DateTime::createFromFormat('Y-W-d','2019-45-09');
var_dump($date);

Output:

bool(false)

Solution:

$date = (new DateTime())->setISODate($year,$week,$day)->setTime($hour,$minute,$second,$microseconds);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    FYI, `W` is not a supported parameter for `createFromFormat`: https://www.php.net/manual/en/datetime.createfromformat.php… – deceze Nov 09 '19 at 08:43
  • 1
    The comments on the manual page link to: http://lornajane.net/posts/2011/getting-dates-from-week-numbers-in-php – deceze Nov 09 '19 at 08:44
  • Thank you, that means I need to explode the string, then create the datetime, is is right? – LF00 Nov 09 '19 at 08:47
  • Yes, `explode` it into three parts and pass them as ISO values. – deceze Nov 09 '19 at 08:51
  • `$date = (new DateTime())->setISODate($year,$week,$day)->setTime($hour,$minute,$second,$microseconds);` – LF00 Nov 09 '19 at 09:05

0 Answers0