运行一个存储在数据库中的时间戳是上周的函数

时间:2014-08-11 10:31:19

标签: php mysql

我正在创建一个插件,我需要检测存储在数据库中的时间戳是前一周。我当前的逻辑是,我从数据库中获取时间戳,并将其与当前日期进行比较,如果是7天,我执行操作,但我一直想检测时间戳是否来自上一周,以便7天逻辑如果时间戳是在上一个星期六和上一周创建的,我的7天检查逻辑将在那里失败。我写的代码是

$date=$active_row->timestamp;
$timestamp_start = $date;
$timestamp_end = strtotime(date('Y-m-d'));
$difference = abs($timestamp_end - $timestamp_start); // that's it!
$days = floor($difference/(60*60*24));

if ($days >7){
    run ()
}

如何检测时间戳是前一周?

3 个答案:

答案 0 :(得分:0)

您可以从这两个日期中提取周数,比较它们并相应地推断出来。

$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W"); // This is your week number

答案 1 :(得分:0)

试试这个

  $day    =  86400;
  $myTime =  $active_row->timestamp ;// or to test use time()-($day*7);


  if (  $myTime <= ( time()-($day*7)  )   )
  {
      echo " one Week or older ";
  }

答案 2 :(得分:0)

您可以在上周一找到$week1 =strtotime("last Monday");

然后

$week2 = str_to_time("+1 week",$week1).

$week1 = date("Y/m/d",$week1);
$week2 = daet("Y/m/d",$week2);

如果您的日期介于这两个日期之间,那么您的情况就是如此。