Home > OS >  PHP-Function to get the first date of a week
PHP-Function to get the first date of a week

Time:06-13

I have a question.

I tried this code to get the first date of a week. It works but only when the number has two digits.

So when the weeknumber is 10 it works but when the weeknumber is 9 or 09 it doesn't. Does anyone here know the error?

$year =2022;
$weeknr = 10;  

$timestamp = strtotime("{$year}-W{$weeknr}");

var_dump($timestamp);
var_dump(date ("d-m-Y", $timestamp));

CodePudding user response:

Quote your weeknr, date("W", $timestamp); returns string

$year = 2022;
$weeknr = "07";  // must be string

$timestamp = strtotime("{$year}-W{$weeknr}");

var_dump($timestamp);
var_dump(date ("d-m-Y", $timestamp));

result:

int(1644796800)
string(10) "14-02-2022"
  • Related