php 一星期前,PHP:周从星期一开始,但是星期日的“星期一星期一”将在下周一星期一开始...

回答(10)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

据我所知,这是一个错误 . 我认为为什么 strtotime('this week'); 应该返回未来的日期没有合理的理由 . 这是一个非常重要的错误 . 在我的特定情况下,我有一个排行榜,显示自本周开始以来用户得分最多的用户 . 但是在星期天,它是空的,因为 strtotime 返回了未来日期的时间戳 . 我很怀疑,因为我还没有找到关于这个bug的任何其他报道 .

伙计们,感谢您的所有时间和帮助 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这个答案很晚,但这是我一直在努力的事情 . 到目前为止,我尝试的每个解决方案都因某种原因而出现故障 . 这就是我最终得到的对我有用的东西 . (虽然它可能看起来很漂亮,但它至少有效) .

$thisMonday = strtotime('next Monday -1 week', strtotime('this sunday'));

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

以下是本周的周一:

echo date("Y-m-d", strtotime(date('o-\\WW')));

e15298c6a3b4591803e154ab0c3b3e2e.png

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

}

e15298c6a3b4591803e154ab0c3b3e2e.png

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.

祝你今天愉快 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我想而不是尝试

echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));

你应该试试

echo date('Y-m-d', strtotime('monday last week'));

e15298c6a3b4591803e154ab0c3b3e2e.png

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

你将得到当周的日期,无论你是星期一,你都会得到那个星期一的日期 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果你想要最近的星期一:

function mostRecentMonday(){

if(date("w") == 1){

return strtotime("midnight today");

} else {

return strtotime("last monday");

}

}

易于修改以使用DateTime,或甚至指定用作基础的不同日期 .

e15298c6a3b4591803e154ab0c3b3e2e.png

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

e15298c6a3b4591803e154ab0c3b3e2e.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值