[英]How to get dates of last 4 weeks Monday and Sunday in php?
I am trying to generate Dates of Monday
and Sunday
of each week
for last 4-8 weeks using PHP's Date function
. 我试图使用PHP的Date function
生成最近4-8周的each week
的Monday
和Sunday
的Date function
。
i have tried getting it like this. 我已经试过像这样。
<?php
$dates_array = array();
$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Last Monday - 3 week"));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Last Sunday - 3 week"));
$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Last Monday - 2 week"));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Last Sunday - 2 week"));
$dates_array['last monday'] = date('Y-m-d',strtotime("Last Monday"));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Last Sunday"));
$dates_array['this monday'] = date('Y-m-d',strtotime("Monday"));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday"));
print_r($dates_array);
OUTPUT: 输出:
Array
(
[-3 week monday] => 2016-05-16
[-4 week sunday] => 2016-05-15
[-2 week monday] => 2016-05-30
[-2 week sunday] => 2016-05-29
[-1 week monday] => 2016-06-06
[-1 week sunday] => 2016-06-05
[last monday] => 2016-06-06
[last sunday] => 2016-06-12
[this monday] => 2016-06-13
[this sunday] => 2016-06-19
)
and output i am getting is all correct for this week and last week, but before last week, everything is messed up, whys is that ? 我得到的输出在本周和上周都是正确的,但是在上周之前,一切都搞砸了,为什么呢?
i even tried like this way. 我什至尝试过这种方式。
<?php
$dates_array = array();
$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('-3 week')));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-3 week')));
$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('-2 week')));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-2 week')));
$dates_array['last monday'] = date('Y-m-d',strtotime("Monday", strtotime('-1 week')));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('-1 week')));
$dates_array['this monday'] = date('Y-m-d',strtotime("Monday"));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday"));
print_r($dates_array);
OUTPUT: 输出:
Array
(
[-3 week monday] => 2016-05-30
[-3 week sunday] => 2016-05-29
[-2 week monday] => 2016-06-06
[-2 week sunday] => 2016-06-05
[last monday] => 2016-06-13
[last sunday] => 2016-06-12
[this monday] => 2016-06-20
[this sunday] => 2016-06-19
)
In above example too ,the output dates are weird. 同样在上面的示例中,输出日期也很奇怪。
so whats the best approach to get Dates of Monday
and Sunday
of each week
for last 4-8 weeks using PHP. 那么使用PHP获取最近4-8周的each week
Monday
和Sunday
的最佳方法是什么。
using : PHP Version 5.3.3 使用:PHP版本5.3.3
UPDATE : 更新:
i have managed to get the proper dates as suggested by jeroen, but using same date function, like this 我设法得到jeroen建议的正确日期,但是使用相同的日期函数,像这样
<?php
$dates_array = array();
$dates_array['-4 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -4 week')));
$dates_array['-4 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -4 week')));
$dates_array['-3 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -3 week')));
$dates_array['-3 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -3 week')));
$dates_array['-2 week monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week -2 week')));
$dates_array['-2 week sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week -2 week')));
$dates_array['last monday'] = date('Y-m-d',strtotime("Monday", strtotime('last week')));
$dates_array['last sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('last week')));
$dates_array['this monday'] = date('Y-m-d',strtotime("Monday", strtotime('this week')));
$dates_array['this sunday'] = date('Y-m-d',strtotime("Sunday", strtotime('this week')));
print_r($dates_array);
OUTPUT: 输出:
Array
(
[-4 week monday] => 2016-05-16
[-4 week sunday] => 2016-05-22
[-3 week monday] => 2016-05-23
[-3 week sunday] => 2016-05-29
[-2 week monday] => 2016-05-30
[-2 week sunday] => 2016-06-05
[last monday] => 2016-06-06
[last sunday] => 2016-06-12
[this monday] => 2016-06-13
[this sunday] => 2016-06-19
)
try the following ways by reducing today with multiples of 7 尝试通过减少今天的7的倍数来尝试以下方法
// This Week
echo date("d-m-Y", strtotime("this Monday"))."<br>";
echo date("d-m-Y", strtotime("this Sunday"))."<br>";
// Last Week
echo date("d-m-Y", strtotime("this Monday -7 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -7 day"))."<br>";
// -2 Week
echo date("d-m-Y", strtotime("this Monday -14 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -14 day"))."<br>";
// -3 Week
echo date("d-m-Y", strtotime("this Monday -21 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -21 day"))."<br>";
// -4 Week
echo date("d-m-Y", strtotime("this Monday -28 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -28 day"))."<br>";
// -5 Week
echo date("d-m-Y", strtotime("this Monday -35 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -35 day"))."<br>";
// -6 Week
echo date("d-m-Y", strtotime("this Monday -42 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -42 day"))."<br>";
// -7 Week
echo date("d-m-Y", strtotime("this Monday -49 day"))."<br>";
echo date("d-m-Y", strtotime("this Sunday -49 day"))."<br>";
Using timestamps and the date function: 使用时间戳和日期函数:
<?php
$timestamp = strtotime('20160604');
$day_of_the_week = date('N', $timestamp); // N returns mon-sun as digits 1 - 7.
$sunday_ts = $timestamp + ( 7 - $day_of_the_week) * 24 * 60 * 60;
$monday_ts = $timestamp - ( $day_of_the_week - 1) * 24 * 60 * 60;
$dates = array();
for($i = 1; $i < 5; $i++) {
$dates_key = '-' . $i . ' Week';
$dates[$dates_key] = array(
'Monday' => date('Y m d', $monday_ts - $i * 7 * 24 * 60 * 60),
'Sunday' => date('Y m d', $sunday_ts - $i * 7 * 24 * 60 * 60)
);
}
printf("Given date %s falls on a %s.\n", date('Y m d', $timestamp), date('l', $timestamp));
var_export($dates);
Output: 输出:
Given date 2016 06 04 falls on a Saturday.
array (
'-1 Week' =>
array (
'Monday' => '2016 05 23',
'Sunday' => '2016 05 29',
),
'-2 Week' =>
array (
'Monday' => '2016 05 16',
'Sunday' => '2016 05 22',
),
'-3 Week' =>
array (
'Monday' => '2016 05 09',
'Sunday' => '2016 05 15',
),
'-4 Week' =>
array (
'Monday' => '2016 05 02',
'Sunday' => '2016 05 08',
),
)
You could this function: 您可以使用以下功能:
function getWeekMonSun($weekOffset) {
$dt = new DateTime();
$dt->setIsoDate($dt->format('o'), $dt->format('W') + $weekOffset);
return array(
'Mon' => $dt->format('Y-m-d'),
'Sun' => $dt->modify('+6 day')->format('Y-m-d'),
);
}
which will return Monday and Sunday of the offset week. 它将返回偏移周的星期一和星期日。
Rather than subtracting days with week, use number of days with current date as shown below. 而不是用星期减去天,而是使用具有当前日期的天数,如下所示。
<?php
date('Y-m-d',strtotime("Monday"));
echo date('Y-m-d',strtotime('last monday days'));
echo date('Y-m-d',strtotime('last monday -7 days'));
echo date('Y-m-d',strtotime('last monday -14 days'));
echo date('Y-m-d',strtotime('last monday -21 days'));
echo date('Y-m-d',strtotime('last monday -28 days'));
?>
I made a table of dates with dates broken down, eg.; 我制作了一个日期表,其中细分了日期。
date - '2005-01-01',
day - 'Saturday',
day-number - 1,
month number -1,
month - 'January',
year number - 2005,
dt formated - 20050101
it from 1st Jan 2005 to 2049. so it makes doing stuff like what you need super simple. 从2005年1月1日到2049年。因此,它使做您需要的事情变得非常简单。 in this case i would simply use; 在这种情况下,我只会使用;
SELECT dt FROM
000_00_Calender
WHEREdt
between CURDATE()-30 and CURDATE() anddt_day
in ('Monday','Sunday') 从000_00_Calender
在CURDATE()-30和CURDATE()之间的dt
和dt_day
中的('Monday','Sunday')中选择dt
if you like, you can get the file here . 如果愿意,可以在这里获取文件。 it is a .sql (1.12mb) 这是一个.sql(1.12mb)
sample; 样品;
INSERT INTO
000_00_Calender
(recID
,dt
,dt_day
,day_number
,month_number
,month_word
,dt_year
,date_formated
,verified
) VALUES (1, '2005-01-01', 'Saturday', 1, 1, 'January', 2005, 20050101, '-'), (2, '2005-01-02', 'Sunday', 2, 1, 'January', 2005, 20050102, '-'), (3, '2005-01-03', 'Monday', 3, 1, 'January', 2005, 20050103, '-'), (4, '2005-01-04', 'Tuesday', 4, 1, 'January', 2005, 20050104, '-'), (5, '2005-01-05', 'Wednesday', 5, 1, 'January', 2005, 20050105, '-'),..... INSERT INTO000_00_Calender
(recID
,dt
,dt_day
,day_number
,month_number
,month_word
,dt_year
,date_formated
,verified
)VALUES(1, '2005-01-01','周六,1,1,2005年,20050101“1月份, '-'),(2,'2005-01-02','Sunday',2,1,'January',2005,20050102,'-'),(3,'2005-01-03','Monday ',3,1,'January',2005,20050103,'-'),(4,'2005-01-04','Tuesday',4,1,'January',2005,20050104,'-') ,(5,'2005-01-05','Wednesday',5,1,'January',2005,20050105,'-'),.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.