简体   繁体   English

上周每天都有PHP strtotime

[英]PHP strtotime last week for every day

Hi I have a query that selects from the database where the 嗨,我有一个查询,该查询从数据库中选择

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

And here is how the $monS and $sunE is defined: 这是$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");
}

It works however most of the time, the rows it counts can be incorrect for the weekends I think.. well staff on my website say it's not counting Saturday and Sundays properly. 但是它在大多数情况下都有效,我认为在周末该行的计数可能不正确。.我网站上的工作人员说,它没有正确地计算周六和周日。 Does my script look okay? 我的脚本看起来还好吗?

It looks like you are trying to get: 看来您正在尝试获得:

Find everything in MySQL in the week preceding this past Sunday. 在上一个星期日之前的一周中,可以在MySQL中找到所有内容。

To do that, I would use the current day of the week to find Sunday, and then subtract a week. 为此,我将使用星期几来查找星期日,然后减去一个星期。

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM