php date 前七天,如何从7天前使用PHP date()返回前一个星期日?(How do I return the previous Sunday from 7 days ago using P...

如何从7天前使用PHP date()返回前一个星期日?(How do I return the previous Sunday from 7 days ago using PHP date()?)

这是我到目前为止:

$date = date('Y-m-d h:i:s', strtotime('-7 days'));

$start = date('Y-m-d h:i:s', strtotime($date,'previous Sunday'));

输出$ start时,返回:1969-12-31 06:00:00

我究竟做错了什么?

Here is what I have so far:

$date = date('Y-m-d h:i:s', strtotime('-7 days'));

$start = date('Y-m-d h:i:s', strtotime($date,'previous Sunday'));

When outputting $start, it returns: 1969-12-31 06:00:00

What am I doing wrong?

原文:https://stackoverflow.com/questions/4730079

更新时间:2019-11-20 00:49

最满意答案

$date需要一个时间戳

$date = strtotime('-7 days');

$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));

$date needs to e a timestamp

$date = strtotime('-7 days');

$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));

2011-01-18

相关问答

因为这不是一项微不足道的任务,所以这是我建议的算法: 创建一个Date对象 从对象获取当周的当天 如果它为零(星期日)则将其设置为7 从日期开始减去许多天* 完成! *由于一些相当聪明的实现,这非常有效。 如果从2月2日开始减去4天,则结果为1月29日。 现在由你来编写一些代码;) Since it's not a trivial task, here's the algorithm I suggest for you: Create a Date object Get the current d

...

看看这里的DATEADD()函数,你可以添加/减去日期的时间段: http://www.w3schools.com/sql/func_dateadd.asp 应该是你需要的。 Have a look at the DATEADD() function here, you can add/subtract a time period from a date: http://www.w3schools.com/sql/func_dateadd.asp Should be what you need.

$date需要一个时间戳 $date = strtotime('-7 days');

$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));

$date needs to e a timestamp $date = strtotime('-7 days');

$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));

使用示例: echo time_elapsed_string('2013-05-01 00:22:35');

echo time_elapsed_string('@1367367755'); # timestamp input

echo time_elapsed_string('2013-05-01 00:22:35', true);

输入可以是任何支持的日期和时间格式 。 输出: 4 months ago

4 months, 2 weeks, 3 days, 1 hour, 49 minute

...

使用%u ,知道需要减去多少天才能到达前一个星期日。 例如像这样: d=2017-10-10 ; u=$(date +%u --date $d) ; date +%Y-%m-%d --date "$d -$u day"

Use %u, to know how many days you need to subtract to get to the previous Sunday. For example like this: d=2017-10-10 ; u=$(date +%u --date

...

而不是时间strtotime('now +30 days')使用strtotime('-30 days') 。 Instead of strtotime('now +30 days') use strtotime('-30 days').

尝试 $start = date( "Y-m-d 11:00:00", strtotime("-2 weeks sunday"));

$end = date( "Y-m-d 11:00:00", strtotime("-1 weeks sunday"));

echo $start . ' - ' . $end;

Try with $start = date( "Y-m-d 11:00:00", strtotime("-2 weeks sunday"));

$end = date( "Y

...

为什么要转换+ $ xdays字符串表示两次 ? 如果你评论你的if语句并添加下一行 echo date('l', strtotime("+$xdays days"));

你可以清楚地看到它的工作原理。 Why do you converting +$xdays string representation twice? If you comment your if statement and add next line echo date('l', strtotime("+$xdays days

...

尝试使用date()函数的'N'格式说明符: date('N')

描述:星期几的ISO-8601数字表示(在PHP 5.1.0中添加) 返回值的示例:1(星期一)至7(星期日) Try to use 'N' format specifier for date() function: date('N')

Description: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) Example

...

select trunc(sysdate-(13-rownum)*7, 'iw') start_dt, trunc(sysdate-(12-rownum)*7, 'iw')-1 end_dt, 'week'||rownum week

from dual

connect by rownum<=90/7+1

select trunc(sysdate-(13-rownum)*7, 'iw') start_dt, trunc(sysdate-(12-rownum)*7, 'iw')-1 end_dt

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值