PHP获取开始日不是星期一的日期的ISO周编号

时间:2014-01-08 02:28:04

标签: php date strtotime iso8601

根据ISO标准,2013年第52周,周六开始,从2013-12-28开始,到2014-01-03(包括)结束。

代码:

echo date('Y-m-d', strtotime("2013-W52-6"));//prints 2013-12-28

现在,我正试图用PHP弄清楚逆。如果我的日期是2014-01-03,如果星期六星期开始,如何知道该日期的ISO周数?

3 个答案:

答案 0 :(得分:0)

使用DateTime类:

  1. createFromFormat()根据您拥有的字符串创建对象
  2. 然后格式化(' W')

答案 1 :(得分:0)

$date = "2013-12-28";
$dayofyear = date("z", $date);

$week = floor($dayofyear / 7) + 1;

// I assume week starts with Monday (I think as ISO does?)
// so only weeks that start on sunday will count as "week 0" instead of "week 1"
// I could be wildly wrong, adjust according to your needs
$first_day = date("w", date("Y", $date) . "-01-01");
if ($first_day == 6)
  --$week;

echo $week;

答案 2 :(得分:-1)

$date = "2013-12-28";
$date_array = explode("-", $date);
$date  = mktime(0, 0, 0, $date_array[1], $date_array[2], $date_array[0]);
$week_number  = date('W', $date);
echo $week_number;

像这样。