Get the “Right” Year of a Certain Date with PHP DateTime

后端 未结 1 405
梦如初夏
梦如初夏 2021-01-23 08:29

I was trying to transform a date \"YYYY/MM/DD\" into \"YYYY/WW\" format, so I can store the weekly aggregation data, which has a structure below

aggre_date(YYYY/         


        
相关标签:
1条回答
  • 2021-01-23 08:45

    You need to use o for the ISO year:

    ISO-8601 year number. 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)

    $dateTime  = new DateTime("2014-12-30");
    echo $dateTime->format("o-W")."\n";
    $dateTime  = new DateTime("2014-01-01");
    echo $dateTime->format("o-W")."\n";
    
    2015-01
    2014-01
    

    Demo

    0 讨论(0)
提交回复
热议问题