How to check if month exist in specific period using PHP - php

How to check if month exist in specific period ($date1 and $date2)
$month = '2016-01';
$date1 = '2016-01-05';
$date2 = '2016-02-04;

First convert the month into a date, like the first day of the month. Then you can compare the dates to check if the month lies in between:
$month_day = date ('Y-m-01', strtotime($month) );
$date1_day = date ('Y-m-01', strtotime($date1) );
$date2_day = date ('Y-m-01', strtotime($date2) );
if ( ($month_day >= min($date1_day, $date2_day))
&& ($month_day <= max($date1_day, $date2_day)) )
{ }

I got a best answer for my question on his link The Answer
This answer get months between two dates
$start = (new DateTime('2016-01-05'))->modify('first day of this month');
$end = (new DateTime('2016-02-04'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$monthsArray = [];
foreach ($period as $dt) {
$monthsArray[] = $dt->format("Y-m"); // I put months exist in this period on array to check later if the $month exist on this array or not
}

You could search the strings using strpos(). http://php.net/manual/en/function.strpos.php
ex:
if (strpos($date1,$month) || strpos($date2,$month)){/* do stuff */}

Related

Get Mondays of every second month

I am trying to get the date of every monday skipping a month each time but I keep getting only the monday of the first week. This is my code:
$begin = new \DateTime("2017-04-01");
$end = new \DateTime("2017-08-31");
$interval = \DateInterval::createFromDateString("next monday of +2 months");
$period = new \DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
{
// echo the date for each monday found in the second month
}
Any help would be greatly appreciated.
You seem to have mixed the $begin and $end values. Also, the DateInterval will only have one Monday every single month. I was not able to find a interval expression to get every Monday in every other month, so I did the filtering manually. In this example we use the month from the begin date, and includes the Mondays from that date, skip two months and so on.
<?php
$end = new \DateTime("2017-04-01");
$begin = new \DateTime("2007-08-31");
$month_even = ((int) $begin->format('m')) % 2 === 0;
$interval = \DateInterval::createFromDateString("next monday");
$period = new \DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
// Check if we want to show even months, make sure that the current month is even
// or, if we want to show odd months, the month should be odd.
if ((((int) $dt->format('m')) % 2 === 0) === $month_even) {
echo $dt->format('d-m-Y') . PHP_EOL;
}
}
Outputs:
31-08-2007
01-10-2007
[...]
06-02-2017
13-02-2017
20-02-2017
27-02-2017

Add datetime Value

I'm searching for the best way to echo day numbers in a week.
First i need to check what week there is.
And then echo all dates of the week into variables.
This is what i got:
//This Week Variable dates
$this_year = date('Y');
$this_week_no = date('W');
$this_week_mon = new DateTime();
$this_week_mon->setISODate($this_year,$this_week_no);
How do i rise tue by one day?
$this_week_tue = $this_week_mon ++;
You can use DateTime::modify():
$this_week_mon->modify('+1 day');
or DateTime::add() which accepts a DateInterval() object:
$this_week_mon->add(new DateInterval('P1D'));
You can loop through all of the days of the week using DatePeriod():
$start = new DateTime();
$start->setISODate($this_year,$this_week_no);
$end = clone $start;
$end->modify('+1 week');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('N');
}
$this_week_mon->modify('+1 day');
Should increment $this_week_mon by one day. To increase by more, just use days;
$this_week_mon->modify('+27 day');
Would increment by 27 days.

Include last date in range between dates in php

I have found a problem with using DatePeriod which might be because I am stupid lol.
As you can see I have added +1 to my end date because I want to include the last date of the range.
But my issue is when I have ending date 31 it makes it to 32 which is not a date so it throws out an error.
Is there a way to include the ending date or to make the +1 work?
$period = new DatePeriod(
new DateTime($event['startyear'].$event['startmonth'].$event['startdate']),
new DateInterval('P1D'),
new DateTime($event['endyear'].$event['endmonth'].$event['enddate'] +1)
);
foreach ($period as $savedDate) {
echo $savedDate;
}
You should create the date object for the initial date (without the +1) and then increment the day of that object by 1 day.
For example:
$date1 = new DateTime($event['endyear'].$event['endmonth'].$event['enddate']);
$date2 = new DateTime($event['endyear'].$event['endmonth'].$event['enddate']);
$date2->modify('+1 day');
$period = new DatePeriod($date1, new DateInterval('P1D'), $date2);
What about this:
$endDate = mktime(0,0,0, $event['endmonth'], $event['enddate'], $event['endyear']);
$counter = 0;
while($endDate >= $date = mktime(0,0,0, $event['startmonth'], $event['startdate']+$counter++, $event['startyear']) ) {
echo date('Y/m/d', $date);
}

Create a table with day of month as headers. PHP

I want to create a table like this...
Oct-01 | Oct-02 | Oct-03 | Oct-04 | etc etc
Using whatever month it is now.
I'm using this to get the first day...
$first = date('01-m-Y',strtotime('this month'));
How can I increment this by the amount of days in this month?
Use cal_days_in_month(CAL_GREGORIAN, date(j), 2013)?
But how to increment the first day.
I tried using '+1 day' in the strtotime, but nothing happened.
Any ideas?
PHP's DateTime class can be used here. You don't need to use cal_days_in_month function to calculate the number of days in a month -- DateTime handles it automatigically. And it's a lot more cleaner and supports a wide range of dates.
Here's how:
$start = new DateTime('first day of this month');
$end = new DateTime('first day of this month + 1 month');
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
foreach($period as $day){
echo $day->format('M-d')."<br/>";
}
Demo!
I prefer using the DateTime object:
$date = new DateTime();
$date->modify('first day of this month');
$days = array();
for($i = 1; $i <= $date->format('t'); $i++){
$days[] = $date->format('M-d');
$date->modify('+1 day');
}
You could also add the whole date object to the array giving you flexibility later to use it however you want.
<?php
$days = array();
$maxDay = cal_days_in_month(CAL_GREGORIAN, date('j'), date('Y'));
for($i = 1; $i<$maxDay; ++$i) {
$days[] = sprintf(date('M')."-%1$02d", $i);
}
echo "<pre>";
var_dump($days);
Now you can do whatever you like with $days array...
http://phpfiddle.org/lite/code/fd1-1r2

How to list dates in yyyy_mm format with PHP using a loop?

What's the cleanest way to use a loop in PHP to list dates in the following way?
2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11
The key elements here:
Should be as simple as possible - I would prefer one for loop instead of two.
Should list this month's date as the first date, and should stop at a fixed point (2009-11)
Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)
Had to make a few tweaks to the solution:
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = date('Y').'-'.date('m').'-01';
// End date
$end_date = '2009-1-1';
while (strtotime($date) >= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
echo substr($date,0,7);
echo "\n";
}
Maybe this little code does the thing? :
more complicated situations.
<?php
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
The credit goes to: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/
This is what im guessing your asking for cause it doesnt really make sense......
$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;
//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
//Second for loop to loop threw months
for($o=$startmonth; $o<=12; $o++) {
//If statement to check and throw stop when at limits
if($i == $endyear && $o <= $endmonth)
echo $i."_".$o."<br/>";
else
break;
}
}
Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7
PHP 5.3 introduces some great improvements to date/time processing in PHP. For example, the first day of, DateInterval and DatePeriod being used below.
$start = new DateTime('first day of this month');
$end = new DateTime('2009-11-01');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y_m') . PHP_EOL;
}

Categories