简体   繁体   English

如何获得相对于一年中的一周的前一年的日期

[英]How to get previous year's date relative to week of year

How can I get the same day of the same ISO-8601 week number but in the previous year using php? 我怎样才能获得相同ISO-8601周数的同一天但是在前一年使用php?

I am familiar with extracting this soft of information from a time stamp. 我熟悉从时间戳中提取这些信息。 Is there a built-in way for me to go from day of week, week of year and year to time stamp? 从一周的一周,一年中的一周和一年到一年的时间戳,我都有内置的方式吗?

You can use the combination of both date and strtotime like so: 您可以使用datestrtotime的组合,如下所示:

// get the timestamp
$ts = strtotime('today');

// get year, week number and day of week
list($year, $week, $dow) = explode('-', date('Y-W-N', $ts));

// use the "YYYY-WXX-ZZ" format    
$format = ($year - 1) . "-W$week-$dow";
echo date('Y-m-d', strtotime($format)), PHP_EOL;

You can do this with strtotime : 你可以用strtotime做到这一点:

$now = time(); // Mon, 12 Nov 2012
$targetYear = date('Y', $now) - 1; // 2011
$targetWeek = date('\\WW-N', $now); // W46-1
$lastYear = strtotime($targetYear . $targetWeek); // Mon, 14 Nov 2011

Try this one: 试试这个:

$date = "2012-11-13";
echo getLastYearWeek($date);
function getLastYearWeek($date)
{
    $last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));

    return $last_year;
}

Or just, 要不就,

$last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));

Use this for reference. 使用参考。

I think you are looking for mktime. 我想你正在寻找mktime。 have a look here http://php.net/manual/en/function.mktime.php 看看http://php.net/manual/en/function.mktime.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM