how to get week in a year php code example

Example 1: php check weekday of date

$dayofweek = date('w', strtotime($date));
$result    = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));

Example 2: php week of a date

Things to be aware of when using week numbers with years.

echo date("YW", strtotime("2011-01-07")); // gives 201101
echo date("YW", strtotime("2011-12-31")); // gives 201152
echo date("YW", strtotime("2011-01-01")); // gives 201152 too


echo date("oW", strtotime("2011-01-07")); // gives 201101
echo date("oW", strtotime("2011-12-31")); // gives 201152
echo date("oW", strtotime("2011-01-01")); // gives 201052 (Year is different than previous example)

Y is year from the date
o is ISO-8601 year number
W is ISO-8601 week number of year

if using 'W' for the week number use 'o' for the year.


Php Example