PHP Get recurring dates for specific weekday - php

How to get all recurring dates for chosen week of the day?
For example, today is Wednesday 15/02/2017 and if I choose Thursday how to get next 5 dates starting next Thursday like:
$dates = array(
[0] => "16/02/2017",
[1] => "23/02/2017",
[2] => "02/03/2017",
[3] => "09/03/2017",
[4] => "16/03/2017");
If I choose Tuesday, get dates starting 21/02/2017, which is next Tuesday

You can use strtotime to generate timestamps for this dates, the date function can generate actual dates.
<?php
// Array for dates
$dates = [];
// Get next thursday
$date = strtotime('thursday');
$dates[] = $date;
// Get the next four
for ($i = 0; $i < 4; $i++)
{
$date = strtotime('+1 week', $date);
$dates[] = $date;
}
// Echo dates
foreach ($dates as $date)
echo date('Y-m-d', $date);

Take a look at this post
Using that idea:
//Get First Thursday
$date = new DateTime();
$date->modify('next thursday');
echo "<br/>Starting Thursday: " . $date->format('d/m/Y');
//Loop over next 4 Thursdays
for($i = 2; $i <= 5; $i++)
{
$date->modify('+7 days');
echo "<br/>Thursday " . $i . ": " . $date->format('d/m/Y');
}

Related

How to dynamically increment dates using strtotime?

I need to get 26 dates from a starting point. The next date starting from the previous one. It would be insane to hard code everything... So I was wondering how can I do this dynamically? Is there a smarter way? I'm looking to increment after the second date. Maybe with a for loop?
<?php
//incrementing dates for bi-weekly (26 periods// 26 dates)
$firstdate = strtotime("+17 days", strtotime("2017-04-03"));//1
$i = date("Y-m-d", $firstdate); echo date("Y-m-d", $firstdate);//echo for testing
echo'<br>';
$seconddate =strtotime("+14 days", strtotime($i));//2
$ii = date("Y-m-d", $seconddate); echo date("Y-m-d", $seconddate);//echo for testing
echo'<br>';
?>
How about this:
// initialize an array with your first date
$dates = array(strtotime("+17 days", strtotime("2017-04-03")));
// now loop 26 times to get the next 26 dates
for ($i = 1; $i <= 26; $i++) {
// add 14 days to previous date in the array
$dates[] = strtotime("+14 days", $dates[$i-1]);
}
// echo the results
foreach ($dates as $date) {
echo date("Y-m-d", $date) . PHP_EOL;
}
Probably the easiest way to do this would be with an array
$myDates = [];
$firstdate = strtotime("+17 days", strtotime("2017-04-03"));
array_push($myDates, date("Y-m-d",$firstdate));
for($i=0;$i<25;$i++){
$lastdate = $myDates[$i];
$nextdate = strtotime("+14 days", strtotime($lastdate));
array_push($myDates,date("Y-m-d",$nextdate));
}
echo "<pre>".var_dump($myDates)."</pre>";

How to get next 10 sundays by giving start date.?

How can i get next 10 Sundays by giving just current data,
Following is my code but it is between date ranges i just want to give start date and get next 10 Sundays from start date(Without end date).
$allweeks=array();
$startDate = date('Y-m-d');
$date = new DateTime($startDate);
$interval = new DateInterval('P2M');
$date->add($interval);
$endDate=$date->format('Y-m-d');
for ($i = strtotime($startDate); $i <= strtotime($endDate); $i = strtotime('+1 day', $i)) {
if (date('N', $i) == 1)
$allweeks[]= date('n-j', $i);
}
foreach($allweeks as $wks)
{
$weeks=explode('-', $wks);
echo '=Week of '.$weeks[0]."/".$weeks[1];
}
you can use DateTime class or strtotime function for same:
Have a look on below code:
Using DateTime Class
$date = '2016-04-05';
$date = new DateTime($date);
// Modify the date it contains
for($i =0; $i < 10; $i++){
$date->modify('next sunday');
// Output
echo $date->format('l : Y-m-d');
echo '<br />';
}
Using strtotime function
$date = '2016-04-05';
// Modify the date it contains
for($i =0; $i < 10; $i++){
$date = date('Y-m-d', strtotime('next sunday', strtotime($date)));
echo date('l : Y-m-d', strtotime($date));
echo '<br />';
}
output
Sunday : 2016-04-10
Sunday : 2016-04-17
Sunday : 2016-04-24
Sunday : 2016-05-01
Sunday : 2016-05-08
Sunday : 2016-05-15
Sunday : 2016-05-22
Sunday : 2016-05-29
Sunday : 2016-06-05
Sunday : 2016-06-12

PHP: DatePeriod with last day of month

I'd like to work with PHP DateInterval to iterate through months:
$from = new DateTime();
$from->setDate(2014, 1, 31);
$period = new DatePeriod($from, new DateInterval('P1M'), 12);
I'd expect it to returns 31 January, 28 February (as the DateInterval is 1 month), but it actually returns 31 January, 3 March, 3 of April... hence skipping February.
Is there any way to do this simply?
Thanks!
EDIT : as a refernece, here is a solution that seems to cover most use cases:
$date = new DateTime('2014-01-31');
$start = $date->format('n');
for ($i = 0; $i < 28; $i++) {
$current = clone $date;
$current->modify('+'.$i.' month');
if ($current->format('n') > ($start % 12) && $start !== 12) {
$current->modify('last day of last month');
}
$start++;
echo $current->format('Y-m-d').PHP_EOL;
}
You can use DateTime::modify():
$date = new DateTime('last day of january');
echo $date->format('Y-m-d').PHP_EOL;
for ($i = 1; $i < 12; $i++) {
$date->modify('last day of next month');
echo $date->format('Y-m-d').PHP_EOL;
}
EDIT: I think I didn't understand your question clearly. Here is a new version:
$date = new DateTime('2014-01-31');
for ($i = 0; $i < 12; $i++) {
$current = clone $date;
$current->modify('+'.$i.' month');
if ($current->format('n') > $i + 1) {
$current->modify('last day of last month');
}
echo $current->format('Y-m-d').PHP_EOL;
}
The issue is cause by the variance between the last day in each of the months within the range. ie. February ending on 28 instead of 31 and the addition of 1 month from the last day 2014-01-31 + 1 month = 2014-03-03 https://3v4l.org/Y42QJ
To resolve the issue with DatePeriod and simplify it a bit, adjust the initial date by resetting the specified date to the first day of the specified month, by using first day of this month.
Then during iteration, you can modify the date period date by using last day of this month to retrieve the bounds of the currently iterated month.
Example: https://3v4l.org/889mB
$from = new DateTime('2014-01-31');
$from->modify('first day of this month'); //2014-01-01
$period = new DatePeriod($from, new DateInterval('P1M'), 12);
foreach ($period as $date) {
echo $date->modify('last day of this month')->format('Y-m-d');
}
Result:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
2014-05-31
2014-06-30
2014-07-31
2014-08-31
2014-09-30
2014-10-31
2014-11-30
2014-12-31
2015-01-31
Then to expand on this approach, in order to retrieve the desired day from the specified date, such as the 29th. You can extract the specified day and adjust the currently iterated month as needed when the day is out of bounds for that month.
Example: https://3v4l.org/SlEJc
$from = new DateTime('2014-01-29');
$day = $from->format('j');
$from->modify('first day of this month'); //2014-01-01
$period = new DatePeriod($from, new DateInterval('P1M'), 12);
foreach ($period as $date) {
$lastDay = clone $date;
$lastDay->modify('last day of this month');
$date->setDate($date->format('Y'), $date->format('n'), $day);
if ($date > $lastDay) {
$date = $lastDay;
}
echo $date->format('Y-m-d');
}
Result:
2014-01-29
2014-02-28 #notice the last day of february is used
2014-03-29
2014-04-29
2014-05-29
2014-06-29
2014-07-29
2014-08-29
2014-09-29
2014-10-29
2014-11-29
2014-12-29
2015-01-29
You may try like this:
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);
I would do it probably like this
$max = array (
31,28,31,30,31,30,31,31,30,31,30,31
); //days in month
$month = 1;
$year = 2014;
$day = 31;
$iterate = 12;
$dates = array();
for ($i = 0;$i < $iterate;$i++) {
$tmp_month = ($month + $i) % 12;
$tmp_year = $year + floor($month+$i)/12;
$tmp_day = min($day, $max[$tmp_month]);
$tmp = new DateTime();
$tmp->setDate($tmp_year, $tmp_month + 1, $tmp_day);
$dates[] = $tmp;
}
var_dump($dates);
This keeps to the same day each month if possible

Get all occurrence of specific day in a month

Suppose i have a month June 2014. Now i want to get dates of all Mondays in June month.
like Monday is coming on following days so answer will be like following
2014-06-02
2014-06-09
2014-06-16
2014-06-23
2014-06-30
please do not give be static solution only for June. I need dynamic solution for every month and purely in PHP.
Try this -
<?php
$startDate = "2014-06-01";
$endDate = "2014-06-30";
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for($i = strtotime('Monday', $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i).PHP_EOL;
DEMO:
http://3v4l.org/n4ULA
Try to create an array with all your date with day on key (with variable $day and $date):
$array = array("Monday" => "2014-06-02", "Tuesday" => "2014-06-03", "Wednesday" => "2014-06-04");
You create a loop to reach all the result :
foreach($array as $key => $value {
if($key == "Monday")
echo $value;
}
Using the above, I created this so you can use variables to define the month, year, and selected weekday.
$month = "6";
$year = "2022";
$weekday = "Tuesday";
$d=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$first_date_of_month = $year."-".$month."-01";
$last_date_of_month = $year."-".$month."-".$d;
$startDate = strtotime($first_date_of_month);
$endDate = strtotime($last_date_of_month);
for($i = strtotime($weekday, $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo "<br />". date('l Y-m-d', $i).PHP_EOL;

Getting the weekday for a certain day in the next 6 months

Is it possible to get the weekday of the 21st of the next 6 months in PHP?
For example, say the 21st falls on a Tuesday next month, then I want "Tuesday" to be returned. But I want this for each of the next 6 months. What is the most elegant solution to this?
Something like that will give you the expected result:
// starting date
$date = new DateTime('2012-08-21');
// iterate for 6 months
for ($i = 0; $i < 6 ; $i++) {
echo $date->format('Y-m-d').' : '.$date->format('l') . PHP_EOL;
$date->modify('+1 month');
}
The DatePeriod and DateInterval classes are super-handy for this sort of thing.
$date = DateTime::createFromFormat('d', 21);
$period = new DatePeriod($date, new DateInterval('P1M'), 6, DatePeriod::EXCLUDE_START_DATE);
foreach ($period as $day) {
echo $day->format('M jS => l'), PHP_EOL;
}
Yes:
<?php
$date = new DateTime;
$date->modify("first day of this month");
$date->modify("+20 days"); //21st
echo $date->format("F: l (Y-m-d)") . PHP_EOL;
for ($i = 0; $i < 6; $i++) {
$date->modify("+1 month");
echo $date->format("F: l (Y-m-d)") . PHP_EOL;
}
Untested, but try this:
$date = 21;
$year = 2012;
$month = date('m'); // Get current month
for($i=0;$i<6;$i++)
{
if($month == 13)
{
$year++;
$month=1;
}
$day = date('l',mktime(0,0,0,$month,$date,$year));
echo "$month $date falls on $day<br />\n";
$month++;
}

Categories