PHP:如何获得过去特定日期的前一个星期日..?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3742820/
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
PHP: How to get previous Sunday of a specific date in the past..?
提问by pnichols
I'm retrieving an entry from a database with a date associated with it.
我正在从数据库中检索一个带有关联日期的条目。
I want to be able to get the previous Sunday and the next Saturday relative to that specific date to populate a jQuery datepicker.
我希望能够获得相对于该特定日期的上一个星期日和下一个星期六来填充 jQuery 日期选择器。
I know how to do it with the actual time/date with :
我知道如何使用实际时间/日期来做:
strtotime('last Sunday')
but I don't know how to do that for a date other than now...
但我不知道如何在现在以外的约会中做到这一点......
Thanks.
谢谢。
回答by leepowers
Use the second argument of strtotime
to specify the date to calculate "last Sunday" and other relative dates from:
使用的第二个参数strtotime
指定计算“上周日”和其他相关日期的日期:
strtotime('last Sunday', strtotime('09/01/2010'));
Example:
例子:
echo date('m/d/Y', strtotime('last Sunday', strtotime('09/01/2010')));
Output:
输出:
08/29/2010
回答by Jeremy
You could also use the datetime class to do this. The code below would work:
您也可以使用 datetime 类来执行此操作。下面的代码可以工作:
$date = new DateTime('09/01/2010'); $date->modify('last Sunday'); echo $date->format('d/m/Y');
Output: 29/08/2010
Have a look at http://ca2.php.net/manual/en/class.datetime.php. The constructor takes two optional arguments, the first of which is a date string for the object (defaulting to now) and the second is a DateTimeZone object (defaults to the time zone set in PHP.ini). The modify method then alters the date using the same expressions that strtotime() support and the format method formats the date using the same format strings as date(). Basically this does the same as pygorex1's example but using object oriented syntax.
看看http://ca2.php.net/manual/en/class.datetime.php。构造函数接受两个可选参数,第一个是对象的日期字符串(默认为现在),第二个是 DateTimeZone 对象(默认为 PHP.ini 中设置的时区)。然后修改方法使用 strtotime() 支持的相同表达式更改日期,格式方法使用与 date() 相同的格式字符串格式化日期。基本上这与 pygorex1 的示例相同,但使用面向对象的语法。
回答by artoodetoo
You can use week day offset. In PHP for arbitrary date:
您可以使用工作日偏移量。在 PHP 中任意日期:
<?php
define('SEC_IN_DAY', (24*60*60));
$d = strtotime('2013-04-15');
// "w" is the day of the week. 0 for Sunday through 6 for Saturday
$delta_sun = -date('w', $d);
$delta_sat = $delta_sun + 6;
echo 'The day ' . date('Y-m-d H:i:s', $d) . "\n";
echo 'Last Sunday '. date('Y-m-d H:i:s', $d + $delta_sun * SEC_IN_DAY) . "\n";
echo 'Next Saturday '. date('Y-m-d H:i:s', $d + $delta_sat * SEC_IN_DAY) . "\n";
The same in MySQL:
在 MySQL 中也是如此:
SET @d := '2013-04-15';
SET @delta_sun := -DATE_FORMAT(@d, '%w');
SET @delta_sat := @delta_sun + 6;
SELECT 'The day' AS `name`, DATE(@d) AS `date`
UNION ALL
SELECT 'Last Sunday' AS `name`, DATE_ADD(@d, INTERVAL @delta_sun DAY) AS `date`
UNION ALL
SELECT 'Next Saturday' AS `name`, DATE_ADD(@d, INTERVAL @delta_sat DAY) AS `date`