php 如何在PHP(多平台)中获取给定周数的第一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1659551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:25:40  来源:igfitidea点击:

How to get the first day of a given week number in PHP (multi-platform)?

php

提问by Amadeus45

What is the simplest way to do it in PHP ?

在 PHP 中最简单的方法是什么?

I want the date of the Monday of a given week number of a year (example : week number 3 of 2009)

我想要一年中给定周数的星期一的日期(例如:2009 年的第 3 周)

Thanks !

谢谢 !

EDIT : If you use Linux only machines, use cletus' solution, however I am looking for something that can work on Windows AND Linux.

编辑:如果您只使用 Linux 机器,请使用 cletus 的解决方案,但是我正在寻找可以在 Windows 和 Linux 上运行的东西。

回答by roshanbh

It's simple on PHP 5.3

在 PHP 5.3 上很简单

echo date('M d',strtotime('2013W15'));

where 15 is the number of week. But for the number below ten make sure it is in the format of 01, 02 for first week and second week.

其中 15 是周数。但是对于十以下的数字,请确保第一周和第二周的格式为 01、02。

回答by Alan Haggai Alavi

Yet another solution:

另一个解决方案:

<?php
        $week = 3;
        $year = 2009;

        $timestamp = mktime( 0, 0, 0, 1, 1,  $year ) + ( $week * 7 * 24 * 60 * 60 );
        $timestamp_for_monday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
        $date_for_monday = date( 'Y-m-d', $timestamp_for_monday );
?>

回答by Jonas

A nice way to get this in a clean way is by using php DateTime class.

以干净的方式获得它的一个好方法是使用 php DateTime 类。

$year = 2015;
$week_no = 1;

$date = new DateTime();
$date->setISODate($year,$week_no);
echo $date->format('d-M-Y'); 

This would result into : 29-12-2014

这将导致:29-12-2014

回答by cletus

You can use strptime()to get the time.

您可以使用strptime()来获取时间。

$time = strptime('1 23 2009', '%w %U %Y');

This will get the time for the Monday (day 1, 0 is Sunday, 6 is Saturday) of the 23rd week of 2009. If you want to format this into a date, use date().

这将获取 2009 年第 23 周的星期一(第 1 天,0 是星期日,6 是星期六)的时间。如果要将其格式化为日期,请使用date().

$date = date('d F Y', $time);

回答by jcromeros1987

This next script gives the 7 days of an specific week of a year

下一个脚本给出了一年中特定一周的 7 天

$time = new DateTime();
$time->setISODate(2016, 13);
for($i=0;$i<7;$i++){
    echo $time->format('d-M-Y') . '<br />';
    $time->add(new DateInterval('P1D'));
}

回答by spf

Seems to be working and not dependent of the server OS :

似乎正在工作并且不依赖于服务器操作系统:

<?php

  $week = 4;
  $year = 2013;

  $timestamp_for_monday = mktime( 0, 0, 0, 1, 1,  $year ) + ((7+1-(date( 'N', mktime( 0, 0, 0, 1, 1,  $year ) )))*86400) + ($week-2)*7*86400 + 1 ;

?>

the idea is to add :

这个想法是添加:

  • the timestamp of the first of January of the chosen year
  • the number of seconds to reach the end of the first week (which is 7 days minus the day of week of the 1st of January + 1 day) multiplied by the number of seconds per day
  • the number of seconds of the number of chosen weeks minus the first week and the current week
  • 1 second to reach the fist second of the current week
  • 所选年份的一月一日的时间戳
  • 到达第一周结束的秒数(即 7 天减去 1 月 1 日的星期几 + 1 天)乘以每天的秒数
  • 所选周数减去第一周和当前周的秒数
  • 1 秒到达本周的第一秒

My example returns : 1358722801 which is the timestamp of 2013/01/21 0:00:01

我的示例返回: 1358722801 这是 2013/01/21 0:00:01 的时间戳

回答by Sunil Soni

I required same in the java script..so i converted.

我在java脚本中要求相同..所以我转换了。

function(week,year){
var timestamp = new Date(year, 0, 1, 0, 0, 0, 0);
var dateObj=new Date();
var val = timestamp.getTime(); 
days=( week * 7 * 24 * 60 * 60*1000 );
val=val+days;
var timestamp_for_monday =val - 86400 *((timestamp.getDay()-1000));
var weekdate=new Date(timestamp_for_monday);
return weekdate;
}

回答by Jason

Here is very simple solution, passing a week no and returns the date.

这是一个非常简单的解决方案,传递一个星期没有并返回日期。

The ISO8601 standard states that week 1 always fall on the week where Jan 4 falls.

ISO8601 标准规定第 1 周总是落在 1 月 4 日所在的那一周。

For example, to get a day in the 4th week of the year:

例如,要获取一年中第 4 周的一天:

$day_in_week = strtotime("2006-01-04 + 4 weeks"));

Then you can adjust this value to Sunday (as a starting place you can guarantee that you can find):

然后可以将此值调整为星期日(作为起点,您可以保证可以找到):

// Find that day's day of the week (value of 0-6)
$wday = date('w', $day_in_week);
$offset = 6 - $wday; // How far it is from Sunday.
$sunday_in_week = $day_in_week - ($offset * (60 * 60 * 24)); // $offset * seconds in a day

Then, you add the seconds in a day again to get Monday.

然后,您再次添加一天中的秒数以获得星期一。

$monday_in_week = $sunday_in_week + (60 * 60 * 24);

Note:This method can occasionally have some problems with daylight savings time. A similar, and slightly safer between DST time changes, method would use the DateTime class. However, DateTime is only support in PHP 5.2.0 or later. The method above works in earlier version as well.

注意:此方法有时会遇到夏令时的一些问题。在 DST 时间更改之间类似且稍微安全的方法将使用DateTime 类。但是,DateTime 仅在 PHP 5.2.0 或更高版本中受支持。上述方法也适用于早期版本。

回答by NawaMan

Try this function

试试这个功能

function MondayOfWeek($WeekNumber, $Year=-1) {
    if ($Year == -1) $Year   = 0+date("Y");
    $NewYearDate             = mktime(0,0,0,1,1,$Year);
    $FirstMondayDate         = 7 + 1 - date("w", mktime(0,0,0,1,1,2009));
    $Dates_fromFirstMonday   = 7 * $WeekNumber;
    $Second_fromFirstMonday  = 60*60*24*($FirstMondayDate + $Dates_fromFirstMonday);
    $MondayDay_ofWeek        = $NewYearDate + $Second_fromFirstMonday;
    $Date_ofMondayDay_ofWeek = 0+date("j", $MondayDay_ofWeek);
    return $Date_ofMondayDay_ofWeek;
}

for($i = 0; $i 

When run, I got:

When run, I got:

-5-12-19-26-2-9-16-23-2-9-16-23-30-6-13-20-27-4-11-18-25-1-8-15-22-29-6-13-20-27-3-10-17-24-31-7-14-21-28-5-12-19-26-2-9-16-23-30-7-14-21-28

Hope this helps.

希望这可以帮助。