Getting first day of every week in 6 months - php

I'm trying to figure out how can I get the the first day of week for the last 6 months and can't get to a working solution.
If I write date("Y-m-d 00:00:00", strtotime("-1 week", date("Y-m-d")); It just subtracts 7 days from the current date; what I want to do is to always return the date of Monday from that week.
Expected result:
2011-8-8 00:00:00
2011-8-1 00:00:00
2011-7-25 00:00:00
2011-7-18 00:00:00
etc

This should do it:
for ($i=0; $i<52/2; $i++)
echo date('Y-m-d', mktime(1, 0, 0, date('m'), date('d')-date('w')-$i*7+1, date('Y'))) . " 00:00:00\n";
it's slightly changed from Mike's Post, who wants the sunday instead of the monday.

I'd recommend DateTime::createFromFormat.
Pre-PHP 5.3, you can use strtotime instead:
<?php
define('NUM_WEEKS', 10);
$dates = Array();
$dates[] = strtotime('Monday');
for ($i = 0; $i < NUM_WEEKS-1; $i++)
$dates[] = strtotime('-1 week', $dates[$i]);
foreach ($dates as $date)
echo strftime('%c', $date) . "\n";
?>
Output:
Mon Aug 22 00:00:00 2011
Mon Aug 15 00:00:00 2011
Mon Aug 8 00:00:00 2011
Mon Aug 1 00:00:00 2011
Mon Jul 25 00:00:00 2011
Mon Jul 18 00:00:00 2011
Mon Jul 11 00:00:00 2011
Mon Jul 4 00:00:00 2011
Mon Jun 27 00:00:00 2011
Mon Jun 20 00:00:00 2011
Live demo.

If you're trying to make Saturday (or any other day for that matter)
the first day of the week to select datasets, here's a good
workaround:
<?php $last_sat=date("z", strtotime("last Saturday"));
$second_last_sat=date("z", strtotime("last Saturday-1 week")); ?>
source: http://www.php.net/manual/en/function.strtotime.php
What you'd probably want is
<?php $last_mon=date("z", strtotime("last Monday "));
$second_last_mon=date("z", strtotime("last Monday-1 week")); ?>
etc..

Related

weird strtotime with GMT which worked perfectly till march 31

Dynamically generating select in a form which works fine for dates less than March 31 but for April 1st and afterwords it is wrong. You can see I am specifying GMT exclusively which worked perfectly at-least for date:March 31.
$today = strtotime("today GMT");
<select name="date">
<option value=<?php echo $d = strtotime('0 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('1 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('2 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<-remainings->
</select>
generated code
28 Mar, 2019-1553731200<--Correct March 28, 2019 12:00:00 AM
29 Mar, 2019-1553817600<--Correct
30 Mar, 2019-1553904000<--Correct
31 Mar, 2019-1553990400<--Correct
01 Apr, 2019-1554073200<--Wrong March 31, 2019 11:00:00 PM (this and remainings should be April <nextday>, 2019 12:00:00 AM)
02 Apr, 2019-1554159600<--Wrong April 1, 2019 11:00:00 PM
03 Apr, 2019-1554246000<--Wrong April 2, 2019 11:00:00 PM
04 Apr, 2019-1554332400<--Wrong April 3, 2019 11:00:00 PM
05 Apr, 2019-1554418800<--Wrong April 4, 2019 11:00:00 PM
06 Apr, 2019-1554505200<--Wrong April 5, 2019 11:00:00 PM
Try changing this $today = strtotime("today GMT"); to this
$today = strtotime("today",gmdate('U'));
When I executed this $today = strtotime("today GMT"); on my system (31-Mar-2019 18:00 time zone EDT), the result was 1553990400 30 Mar, 2019 20:00
I read the PHP: Datetime Relative Formats Doc and could find no indication that time zone is used in any format, which is why I tried it with gmdate('U').
This code:
echo "\ngmdate\n";
echo "current date: ",strtotime("today"),"<-- ",date('d M, Y H:i'),"\n";
echo "'today GMT': ",strtotime("today GMT"),"<--",date('d M, Y H:i',strtotime("today GMT")),"\n\n";
$todayGMdate = strtotime("today",gmdate('U'));
echo $todayGMdate,"<-- ",date('d M, Y H:i',$todayGMdate),"\n";
for ($i = 0; $i < 10; $i++) {
$d=strtotime("+$i day",$todayGMdate);
echo date('d M, Y', $d).'-'.$d," <-- ",date('d M, Y H:i',$d),"\n";
}
Produces this result:
gmdate
current date: 1554091200<-- 01 Apr, 2019 10:44
'today GMT': 1554076800<--31 Mar, 2019 20:00
1554091200<-- 01 Apr, 2019 00:00
01 Apr, 2019-1554091200 <-- 01 Apr, 2019 00:00
02 Apr, 2019-1554177600 <-- 02 Apr, 2019 00:00
03 Apr, 2019-1554264000 <-- 03 Apr, 2019 00:00
04 Apr, 2019-1554350400 <-- 04 Apr, 2019 00:00
05 Apr, 2019-1554436800 <-- 05 Apr, 2019 00:00
06 Apr, 2019-1554523200 <-- 06 Apr, 2019 00:00
07 Apr, 2019-1554609600 <-- 07 Apr, 2019 00:00
08 Apr, 2019-1554696000 <-- 08 Apr, 2019 00:00
09 Apr, 2019-1554782400 <-- 09 Apr, 2019 00:00
10 Apr, 2019-1554868800 <-- 10 Apr, 2019 00:00
It seems like strtotime("today GMT") gets start of today in current locale, and then adds the gmt offset.
I suspect this note from the doc is in play:
Note:
Relative statements are always processed after non-relative
statements. This makes "+1 week july 2008" and "july 2008 +1 week"
equivalent.
Exceptions to this rule are: "yesterday", "midnight", "today", "noon"
and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are
different. Considering today's date of "July 23rd, 2008" the first one
produces "2008-07-24 11:00" where as the second one produces
"2008-07-24 00:00". The reason for this is that those five statements
directly influence the current time.

Remove weekend from array of the next 10 days

I have created an array of the next 10 days, with a 2 days buffer (i.e. if it is a Monday, the array starts on Wednesday). I am now trying to remove weekends from my array but unsure how to go about doing this. Below is my PHP and the returned array:
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This returns:
Array (
[1548192409] => Tuesday, 22nd Jan
[1548365209] => Thursday, 24th Jan
[1548538009] => Saturday, 26th Jan
[1548710809] => Monday, 28th Jan
[1548883609] => Wednesday, 30th Jan
[1549056409] => Friday, 1st Feb
[1549229209] => Sunday, 3rd Feb
[1549402009] => Tuesday, 5th Feb
[1549574809] => Thursday, 7th Feb
[1549747609] => Saturday, 9th Feb
)
Can somebody help me understand how I would filter out any Saturdays or Sundays from the above
http://php.net/manual/en/function.date.php
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
if (!in_array(date('w',$date_buffer), [0,6])) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
}
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This is a good job for the DatePeriod class. We set up a period of 10 recurrences of 2 days from the start time (in 2 days), and then can iterate through the dates, checking for a weekend day (day of week = 0 or 6) to exclude them from the output:
$start = new DateTime('+2 days');
$period = new DatePeriod($start, new DateInterval('P2D'), 9);
foreach ($period as $date) {
$dow = (int)$date->format('w');
if ($dow != 0 && $dow != 6) {
$days[$date->format('U')] = $date->format('l, jS M');
}
}
print_r($days);
Output:
Array (
[1548194036] => Tuesday, 22nd Jan
[1548366836] => Thursday, 24th Jan
[1548712436] => Monday, 28th Jan
[1548885236] => Wednesday, 30th Jan
[1549058036] => Friday, 1st Feb
[1549403636] => Tuesday, 5th Feb
[1549576436] => Thursday, 7th Feb
)
If you wanted 10 consecutive days (excluding weekends) from 2 days from today, you would just change the second line of the code to:
$period = new DatePeriod($start, new DateInterval('P1D'), 9);
and the output would be:
Array (
[1548197829] => Tuesday, 22nd Jan
[1548284229] => Wednesday, 23rd Jan
[1548370629] => Thursday, 24th Jan
[1548457029] => Friday, 25th Jan
[1548716229] => Monday, 28th Jan
[1548802629] => Tuesday, 29th Jan
[1548889029] => Wednesday, 30th Jan
[1548975429] => Thursday, 31st Jan
)
Demo on 3v4l.org
Here is a simple answer using the while loop.
https://3v4l.org/0lpGX
<?php
$x = 1; // Start
$y = 10; // Iterations Needed
$days = []; //Empty Array
while($x <= $y) {
// Set Buffer
$buffer = 2 + $x;
// Get Date with Buffer
$date = date(strtotime("+$buffer days"));
// If the day is a weeday
if(date('N', $date) < 6){
// Add to array
$days[$date] = date("l, jS M", $date);
// If not, increase max iteration (example: 10 to 11)
}else{
$y++;
}
// Go to next loop
$x++;
}
echo "<pre>";
print_r($days);
?>
Which prints out
Array
(
[1548283397] => Wednesday, 23rd Jan
[1548369797] => Thursday, 24th Jan
[1548456197] => Friday, 25th Jan
[1548715397] => Monday, 28th Jan
[1548801797] => Tuesday, 29th Jan
[1548888197] => Wednesday, 30th Jan
[1548974597] => Thursday, 31st Jan
[1549060997] => Friday, 1st Feb
[1549320197] => Monday, 4th Feb
[1549406597] => Tuesday, 5th Feb
)

PHP get a list of dates Mon - Sun

I would like to get a list of dates in the following format for a given year;
array:4 [▼
"04" => "25 Jan 2016 - 31 Jan 2016"
"03" => "18 Jan 2016 - 24 Jan 2016"
"02" => "11 Jan 2016 - 17 Jan 2016"
"01" => "4 Jan 2016 - 10 Jan 2016"
]
The array key shows the week number. The array value shows the dates.
If the year given is equal to the current year then I only want it to show dates up until the current week.
How can I achieve this?
This user defined getDates() function which takes weeknumber and year as input will return the desired result.
<?php
echo "<pre>";
print_r(getDates('4', '2016'));
echo "</pre>";
function getDates($weekNumber, $year) {
$returnArr = array();
for($i=$weekNumber; $i >= 1; $i--) {
$wno = $i<=9 ? "0".$i : $i;
$calStr = $year . "W" . $wno;
$startDate = date('d M Y',strtotime($calStr));
$endDate = date('d M Y', strtotime($startDate. ' + 6 day'));
$weekDays = $startDate . ' - ' . $endDate;
$returnArr[$wno] = $weekDays;
}
return $returnArr;
}
Output
Array
(
[04] => 25 Jan 2016 - 31 Jan 2016
[03] => 18 Jan 2016 - 24 Jan 2016
[02] => 11 Jan 2016 - 17 Jan 2016
[01] => 04 Jan 2016 - 10 Jan 2016
)

Weekly date range in between years in php

can you help me on how to get the weekly range in between years.
For example:
Date Selected: Dec 16 , 2014 to Jan 15,2015
Weekly Range Should be:
Dec 16, 2014 to Dec 21,2014
Dec 22, 2014 to Dec 28,2014
Dec 29, 2014 to Jan 4, 2015
Jan 5, 2015 to Jan 11,2015
Jan 12, 2015 to Jan 15, 2015
There is a similar solution
Get all week start date and end date within a date range in php
But this doesn't work in between years, it gives incorrect weekly range.
try using this code
$p = new DatePeriod(
new DateTime('2014-16-12'),
new DateInterval('P1W'),
new DateTime('2015-15-01')
);
$dates=array();
foreach ($p as $w) {
$date_week= $w->format('Y-m-d');
$date_week_7= strtotime($date_week);
$dates[] =date('Y-m-d',strtotime("+7 day", $date_week_7));
}
$dates array will contain all the week days between the dates

Timestamp of nearest valid month

Just a quickie..
How to derive the unixtime of nearest March or June?
If the current month is February of 2009, the script should give the unixtime of March 01, 2009.
If the current month is April of 2009, the script should give the unixtime of June 01, 2009.
If the current month is October of 2009, the script should give the unixtime of March 01, 2010.
Thank you for any help!
Update: Sorry, my bad. "next" works with days and in cases like "next month" but not "next March" so it's a little more convoluted than the original one liner.
strtotime() is really awesome for things like this:
$tests = array(
strtotime('2 february'),
strtotime('4 april'),
strtotime('9 november'),
);
foreach ($tests as $test) {
echo date('r', $test) . ' => ' . date('r', nextmj($test)) . "\n";
}
function nextmj($time = time()) {
$march = strtotime('1 march', $time);
$june = strtotime('1 june', $time);
if ($march >= $time) {
return $march;
} else if ($june >= $time) {
return $june;
} else {
return strtotime('+1 year', $march);
}
}
Output:
Mon, 02 Feb 2009 00:00:00 +0000 => Sun, 01 Mar 2009 00:00:00 +0000
Sat, 04 Apr 2009 00:00:00 +0000 => Mon, 01 Jun 2009 00:00:00 +0000
Mon, 09 Nov 2009 00:00:00 +0000 => Mon, 01 Mar 2010 00:00:00 +0000
Also see What date formats does the PHP function strtotime() support?

Categories