PHP strtotime上周每天都有

时间:2014-10-08 19:49:49

标签: php mysql strtotime

您好我有一个查询从数据库中选择

`timestamp` >= '".$monS."' AND `timestamp` < '".$sunE."'

以下是$monS$sunE的定义方式:

$startTime = "00:00:00";
$endTime = "23:59:59";

$today = date('l', strtotime("now"));

if($today == "Monday"){
    $monS = strtotime("yesterday -7 days $startTime");
    $sunE = strtotime("yesterday -1 days $endTime");
}
    if($today == "Tuesday"){
    $monS = strtotime("yesterday -8 days $startTime");
    $sunE = strtotime("yesterday -2 days $endTime");
}
if($today == "Wednesday"){
    $monS = strtotime("yesterday -9 days $startTime");
    $sunE = strtotime("yesterday -3 days $endTime");
}
if($today == "Thursday"){
    $monS = strtotime("yesterday -10 days $startTime");
    $sunE = strtotime("yesterday -4 days $endTime");
}
if($today == "Friday"){
    $monS = strtotime("yesterday -11 days $startTime");
    $sunE = strtotime("yesterday -5 days $endTime");
}
if($today == "Saturday"){
    $monS = strtotime("yesterday -12 days $startTime");
    $sunE = strtotime("yesterday -6 days $endTime");
}
if($today == "Sunday"){
    $monS = strtotime("yesterday -13 days $startTime");
    $sunE = strtotime("yesterday -7 days $endTime");
}

但是大部分时间都可以使用,我认为周末的行数可能不正确。我的网站上的工作人员说它不适合星期六和星期日。我的脚本看起来不错吗?

1 个答案:

答案 0 :(得分:2)

看起来你想要得到:

  

在上周日前一周查找MySQL中的所有内容。

要做到这一点,我会使用当周的当天查找星期日,然后减去一周。

$dow = date('N'); // current DOW – 0 = Sunday, 1 = Monday, 2 = Tues...
$now = time();
$sunE = $now - (24 * 3600 * $dow); // 24 * 3600 = one day in seconds
$monS = $sunE - (24 * 3600 * 7);