dpy33121 2012-09-14 12:21
浏览 56
已采纳

按日期过滤php本月和过去x天和本周

I have a date that returns in a string as 2012-03-19 05:00:32, its not coming from the database

I can use below to search for the last 30 days

$date = '2012-03-19 05:00:32';
if (strtotime($date) >= strtotime('-7 days')) {
// do something
}

Problem is if today is the 19th March, i was to search from the 11th to the 18th for the last 7 days and that seems to search for the last 7 days by calculating 24 hours * 7 by my searches need to start from 00:00:01 each day.

My plan is to break the date down into Year, Month and Day then check if year = 12, then check if month = 3, then check if date between 11 and 18.

Im just wondering if there is a more efficient way to do this or if im on the right track.

I also have the same issue with running a search on all info from this month and also want to search for all info this week starting on Monday.

So this is just asking if my method is sound or if there is a more efficient method.

  • 写回答

2条回答 默认 最新

  • douyi1944 2012-09-14 12:33
    关注
    $mytime = new DateTime('2012-03-19 05:00:32');
    $mydate = new DateTime($mytime->format('Y-m-d')); //keep date only, exclude the time component
    $now=new DateTime; //includes hours, minutes, seconds
    $today=new DateTime($now->format('Y-m-d')); //time set to 0:00
    
    $interval = $mydate->diff($today);
    if($interval->format('d') <=7) { //assuming that $mydate isn't in the past
     //do something
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度