PHP:如何在给定日期输入的情况下获得星期日和星期六?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1485480/
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 02:46:40  来源:igfitidea点击:

PHP: How to get Sunday and Saturday given a date input?

phpalgorithmdatetime

提问by marknt15

How can I get the Sunday and Saturday of the week given a specific date?

如何获得给定特定日期的一周中的星期日和星期六?

For example:

例如:

input: Monday, September 28, 2009

output should be:

Sunday, September 27, 2009 12:00 AM - Saturday, October 3, 2009 11:59 PM

输入:2009 年 9 月 28 日,星期一

输出应该是:

2009 年 9 月 27 日星期日上午 12:00 - 2009 年 10 月 3 日星期六晚上 11:59

I am thinking of using the date, strtotime, mktimeand timephp functions.

我想用的datestrtotimemktimetimePHP函数。

If you have a usable function then it is also fine with me.

如果您有可用的功能,那么我也可以。

Thanks in advance :)

提前致谢 :)

Cheers, Mark

干杯,马克

回答by cletus

You use strtotime()and date():

你使用strtotime()date()

<?php
$s = 'Monday, September 28, 2009';
$time = strtotime($s);
$start = strtotime('last sunday, 12pm', $time);
$end = strtotime('next saturday, 11:59am', $time);
$format = 'l, F j, Y g:i A';
$start_day = date($format, $start);
$end_day = date($format, $end);
header('Content-Type: text/plain');
echo "Input: $s\nOutput: $start_day - $end_day";
?>

outputs:

输出:

Input: Monday, September 28, 2009
Output: Sunday, September 27, 2009 12:00 PM - Saturday, October 3, 2009 11:59 AM

回答by cletus

<?php

date_default_timezone_set('Europe/London');

$datetime = new DateTime("2009-09-23 12:00:00");

// Saturday
$datetime->setISODate(2009, $datetime->format("W"), 6);
print "Saturday:" . $datetime->format(DATE_ATOM) . "\n";
// Sunday
$datetime->setISODate(2009, $datetime->format("W"), 0);
print "Sunday: " . $datetime->format(DATE_ATOM) . "\n";


?>

回答by RageZ

take a look at strtotime

看看strtotime

i.e.

IE

strtotime('next sunday', strtotime($your_date_string));

回答by garrizaldy

echo date("Y-m-d H:i:s", strtotime("last sunday", time()));

echo date("Ymd H:i:s", strtotime("last sunday", time()));

echo date("Y-m-d H:i:s", strtotime("next saturday", time()));

echo date("Ymd H:i:s", strtotime("下周六", time()));

回答by Warren Landis

the issue with the 'correct' answer is, what if the supplied date is Sunday? Then it will give you the previous Sunday, instead of the current Sunday. Same issue with Saturday.

“正确”答案的问题是,如果提供的日期是星期日怎么办?然后它会给你上一个星期天,而不是当前的星期天。周六也有同样的问题。

$s = 'Monday, September 28, 2009';
$time = strtotime($s);
if(date('D', $time) == "Sun"){
    $start = strtotime(' 12pm', $time);
}
else {
    $start = strtotime('last sunday, 12pm', $time);
}
if(date('D', $time) == "Sat"){
    $start = strtotime(' 12pm', $time);
}
else {
    $start = strtotime('next saturday, 12pm', $time);
}
$format = 'l, F j, Y g:i A';
$start_day = date($format, $start);
$end_day = date($format, $end);
header('Content-Type: text/plain');
echo "Input: $s\nOutput: $start_day - $end_day";

回答by Kavita Mantri

Get Sunday Php Code:

获取周日 PHP 代码:

echo "Today Is : " . date("d-m-Y"); 
$dw = date( "w");
$dw = 7-$dw;
echo "\nNext Sunday Is" .date('d-m-Y', strtotime("+$dw days")); 

回答by Daniel Iser

This is what your looking for. It is what I use.

这就是你要找的。这是我使用的。

$beginningOfWeek = date('Y-m-d H:i:s', strtotime('last Sunday'));
$endOfWeek = date('Y-m-d H:i:s', strtotime('+6 day', strtotime('last Sunday')) );

回答by Norman

You may find my Date Time Helper much handy in this case , it can finish in 3 Lines

在这种情况下,您可能会发现我的 Date Time Helper 非常方便,它可以在 3 行中完成

http://normandqq.github.io/Date-Time-Helper/

http://normandqq.github.io/Date-Time-Helper/

    $date = '2013-08-11';
    $monday = Model_DTHpr::getMondayByDate($date);
    $sunday = Model_DTHpr::adjustDays("add",$monday,6);

Out Put:

输出:

    2013-08-05 //Monday
    2013-08-11 //Sunday

回答by Palo Verde

strtotime

时间

$input = strtotime("Monday, September 28, 2009");
$nextSunday = strtotime("next Sunday",$input);

回答by meder omuraliev

<?php

$date = strtotime('Monday, September 28, 2009 - 1 day');
$initialString =  date('l, F d, Y g:i A', $date);
$end = date('l, F d, Y g:i A', strtotime( 'next saturday 11:59 pm', $date));

echo $initialString . ' - ' . $end; 

output: Sunday, September 27, 2009 12:00 AM - Saturday, October 03, 2009 11:59 PM

输出: 2009 年 9 月 27 日星期日上午 12:00 - 2009 年 10 月 3 日星期六晚上 11:59