回答(10)
2 years ago
据我所知,这是一个错误 . 我认为为什么 strtotime('this week'); 应该返回未来的日期没有合理的理由 . 这是一个非常重要的错误 . 在我的特定情况下,我有一个排行榜,显示自本周开始以来用户得分最多的用户 . 但是在星期天,它是空的,因为 strtotime 返回了未来日期的时间戳 . 我很怀疑,因为我还没有找到关于这个bug的任何其他报道 .
伙计们,感谢您的所有时间和帮助 .
2 years ago
这个答案很晚,但这是我一直在努力的事情 . 到目前为止,我尝试的每个解决方案都因某种原因而出现故障 . 这就是我最终得到的对我有用的东西 . (虽然它可能看起来很漂亮,但它至少有效) .
$thisMonday = strtotime('next Monday -1 week', strtotime('this sunday'));
2 years ago
以下是本周的周一:
echo date("Y-m-d", strtotime(date('o-\\WW')));
2 years ago
这不是理想的,但这是我使用的方法:
if(date('N') == 7) {
$date = date('Y-m-d',strtotime('monday last week'));
} else {
$date = date('Y-m-d',strtotime('monday this week'));
}
2 years ago
I think the only problem with your coding is TimeZone.
Solution:
设置您自己的时区 . 以下是我自己时区的示例:
Example
date_default_timezone_set('Asia/Kolkata');
Set the above line before calling any time function.
祝你今天愉快 .
2 years ago
我想而不是尝试
echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
你应该试试
echo date('Y-m-d', strtotime('monday last week'));
2 years ago
试试这个代码
// set current date
$date = date("m/d/Y");
$ts = strtotime($date); // also $ts = time();
// find the year and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
$i = 1; // 1 denotes the first day of week
$ts = strtotime($year.'W'.$week.$i);
echo $day = date("l", $ts); // generate the name of day
echo "
";
echo $day = date("Y-m-d", $ts); // generate the date
你将得到当周的日期,无论你是星期一,你都会得到那个星期一的日期 .
2 years ago
如果你想要最近的星期一:
function mostRecentMonday(){
if(date("w") == 1){
return strtotime("midnight today");
} else {
return strtotime("last monday");
}
}
易于修改以使用DateTime,或甚至指定用作基础的不同日期 .
2 years ago
$first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
$last_week_date = date('d F Y', strtotime('next Monday -1 week + 6 days', strtotime('this sunday')));
2 years ago
这是为了寻找适合任何一天的友好解决方案 .
function getWeekStart($week_start_day = "Monday") {
$week_days = array("Sunday"=>0,"Monday"=>1,"Tuesday"=>2,"Wednesday"=>3,"Thursday"=>4,"Friday"=>5,"Saturday"=>6,);
if(!isset($week_days[$week_start_day])) {
return false;
} else {
$start_day = $week_days[$week_start_day];
$today = date("w");
$one_day = (60 * 60 * 24);
if($today < $start_day) {
$days_difference = 7 - ($start_day - $today);
} else {
$days_difference = ($today - $start_day);
}
$week_starts = strtotime(date("Y-m-d 00:00:00")) - ($one_day * $days_difference);
return $week_starts;
}
}
//Test: If today is Monday, it will return today's date
echo date("Y-m-d H:i:s", getWeekStart("Monday"));