In my application a week is defined from Monday 12:00:00 AM to Sunday 11:59:59 PM
在我的申请中,一个星期的定义是星期一12:00 AM到星期日11:59:59 PM。
Whenever a user visits my site - I need to find the previous weeks date range and show him results based on that. It sounds simple but I'm lost.
每当用户访问我的站点时——我需要找到前几周的日期范围,并基于此向他显示结果。听起来很简单,但我迷路了。
To give you scenarios - - March 1st Monday 12:00:00 AM to March 7th Sunday 12:59:59 PM is the week.
3月1日星期一12:00:00到3月7日星期日12:59:59是星期。
Now when a user visits the website on 8th March or 10th March or 12th March - based on the current date I should be able to get the previous week date range ie start date March 1st and end date March 7th.
现在,当用户在3月8日或3月10日或3月12日访问网站时——基于当前日期,我应该能够获得之前的周日期范围(3月1日开始日期和3月7日结束日期)。
But if the user visits the site say on 16th March - the date range I would need is March 8th to March 15th.
但是如果用户在3月16日访问这个网站——我需要的日期范围是3月8日到3月15日。
How can I do this in PHP. Thanks
如何在PHP中实现这一点。谢谢
5 个解决方案
#1
8
You could try doing it with timestamps, but that gets messy with timezone changes (for example, CET -> CEST). I'd use the DateTime
class:
您可以尝试使用时间戳来完成它,但是这在时区更改(例如,CET -> CEST)中会变得很混乱。我会使用DateTime类:
$d = new DateTime();
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$d->modify("-$diff day");
echo $d->format('Y-m-d') . ' - ';
$d->modify('+6 day');
echo $d->format('Y-m-d');
#2
8
The strtotime
function is very handy here:
strtotime函数在这里非常方便:
$mondayStr = "last monday";
if (date('N') !== '1') { // it's not Monday today
$mondayStr .= " last week";
}
$monday = strtotime($mondayStr);
echo date('r', $monday); // Mon, 22 Feb 2010 00:00:00 +1000
$sunday = strtotime('next monday', $monday) - 1;
echo date('r', $sunday); // Sun, 28 Feb 2010 23:59:59 +1000
#3
1
function get_week_start($year, $month, $day)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date('F j Y', $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
}
You could perhaps add the next 6 days and you have it.
你可以把接下来的6天加起来。
#5
0
GMT version
格林尼治时间版本
$prev_monday_t = time() - (gmdate('N') + 6) * 86400;
$prev_sunday_t = time() - gmdate('N') * 86400;
echo gmdate('Y-m-d H:i:s', $prev_monday_t ).' '.gmdate('Y-m-d H:i:s', $prev_sunday_t );
Local version
本地版本
$prev_monday_t = time() - (date('N') + 6) * 86400;
$prev_sunday_t = time() - date('N') * 86400;
echo date('Y-m-d H:i:s', $prev_monday_t ).' '.date('Y-m-d H:i:s', $prev_sunday_t );
#1
8
You could try doing it with timestamps, but that gets messy with timezone changes (for example, CET -> CEST). I'd use the DateTime
class:
您可以尝试使用时间戳来完成它,但是这在时区更改(例如,CET -> CEST)中会变得很混乱。我会使用DateTime类:
$d = new DateTime();
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$d->modify("-$diff day");
echo $d->format('Y-m-d') . ' - ';
$d->modify('+6 day');
echo $d->format('Y-m-d');
#2
8
The strtotime
function is very handy here:
strtotime函数在这里非常方便:
$mondayStr = "last monday";
if (date('N') !== '1') { // it's not Monday today
$mondayStr .= " last week";
}
$monday = strtotime($mondayStr);
echo date('r', $monday); // Mon, 22 Feb 2010 00:00:00 +1000
$sunday = strtotime('next monday', $monday) - 1;
echo date('r', $sunday); // Sun, 28 Feb 2010 23:59:59 +1000
#3
1
function get_week_start($year, $month, $day)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date('F j Y', $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
}
You could perhaps add the next 6 days and you have it.
你可以把接下来的6天加起来。
#4
#5
0
GMT version
格林尼治时间版本
$prev_monday_t = time() - (gmdate('N') + 6) * 86400;
$prev_sunday_t = time() - gmdate('N') * 86400;
echo gmdate('Y-m-d H:i:s', $prev_monday_t ).' '.gmdate('Y-m-d H:i:s', $prev_sunday_t );
Local version
本地版本
$prev_monday_t = time() - (date('N') + 6) * 86400;
$prev_sunday_t = time() - date('N') * 86400;
echo date('Y-m-d H:i:s', $prev_monday_t ).' '.date('Y-m-d H:i:s', $prev_sunday_t );