DateTime的YearWeek [英] YearWeek from DateTime

查看:57
本文介绍了DateTime的YearWeek的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PHP中的DateTime获取YearWeek:

I would like to get YearWeek from DateTime in PHP:

$day = \DateTime ...
$YearWeek = date("YW", date_timestamp_get($day));

但是如果一周是两年,它将不起作用:

But in case the week is on two years, it does not work:

$day // 2017-01-01
$YearWeek // I get 201752 instead of 201652

我该怎么办?

推荐答案

date()的手册中,使用 o 而不是 Y

From the manual for date(), use o instead of Y:


ISO-8601周编号年份。它的值与Y相同,但
除外,如果ISO周编号(W)属于上一年或下一年,则改用该年的
。 (在PHP 5.1.0中添加)

ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)



$day = strtotime('2017-01-01');
echo date("YW", $day);  //201752
echo date("oW", $day);  //201652

这篇关于DateTime的YearWeek的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆