如何查找给定日期所属一周的第一天?

发布于 2024-11-18 04:17:46 字数 608 浏览 3 评论 0原文

可能的重复:
用 PHP 获取一周的第一天?

给定一个时间戳,我需要查找时间戳所属周的第一天。

例如,

$format = "first day of week";

//convert to time
$time = strtotime("2011-07-01 00:00:00");

//format the time to the first day of the week
$date = strtotime($format,$time);

//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);

一周的开始应该等于 2011-06-27,此时它出了严重的错误,等于 1970-01-01,这对我来说意味着“一周的第一天”格式是无效的。

任何想法将不胜感激,谢谢,

Possible Duplicate:
Get first day of week in PHP?

Given a timestamp I need to find the first day the week the timestamp belongs to.

e.g.

$format = "first day of week";

//convert to time
$time = strtotime("2011-07-01 00:00:00");

//format the time to the first day of the week
$date = strtotime($format,$time);

//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);

The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.

Any ideas would be much appreciated thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-11-25 04:17:46
$time = strtotime("2011-07-01 00:00:00");

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
$time = strtotime("2011-07-01 00:00:00");

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
享受孤独 2024-11-25 04:17:46

测试一下:

$time=[Your Request Time];

$dayofweek = date("w",strtotime($time));

if( $dayofweek != 0  )

    $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );

else

    $firstdayOfWeek = strtotime($time);

Test it :

$time=[Your Request Time];

$dayofweek = date("w",strtotime($time));

if( $dayofweek != 0  )

    $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );

else

    $firstdayOfWeek = strtotime($time);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文