gpt4 book ai didi

PHP strtotime 上周的每一天

转载 作者:行者123 更新时间:2023-11-29 07:51:41 30 4
gpt4 key购买 nike

嗨,我有一个查询,它从数据库中选择

`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");
}

它在大多数情况下都有效,我认为周末计算的行数可能不正确..我网站上的工作人员说它没有正确计算周六和周日。我的脚本看起来还好吗?

最佳答案

看起来您正在尝试获取:

Find everything in MySQL in the week preceding this past Sunday.

为此,我将使用一周中的当前日期来查找星期日,然后减去一周。

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

关于PHP strtotime 上周的每一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26265227/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com