Include last date in range between dates in php - 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);
}

Related

How to check if month exist in specific period using 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 */}

PHP >> Date Period Foreach Won't run

Can't work out what I'm missing here, can anyone help?
This should be simple, GET start_date & end_date, create DatePeriod() with interval 1 day, loop through.
but it won't work for some reason?
URL which loads is:
/forms/process_table_cumulative_averages.php?start_date=12-05-2015&end_date=19-05-2015&team_id=all
Code is as follows:
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->createFromFormat('d/m/Y', $_GET['start_date']);
$end_date->createFromFormat('d/m/Y', $_GET['end_date']);
$team_id = $_GET['team_id'];
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start_date, $interval, $end_date);
foreach ( $period as $datetime ){
echo "hi\n";
}
Output is 204 No Content
:( Thanks
createFromFormat() is a static function that returns DateTime instance, so you must assign the result to your start and end variables:
$start_date = DateTime::createFromFormat('d-m-Y', $_GET['start_date']);
$end_date = DateTime::createFromFormat('d-m-Y', $_GET['end_date']);

Getting dates from an array

I found the following code from another post which gives me what I want except I would like to be able to grab each day as a variable so that I can use them in form fields.
Can anyone tell me how to achieve this?
<?php
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
?>
The results of the above are this:
2015-02-16
2015-02-17
2015-02-18
2015-02-19
2015-02-20
2015-02-21
2015-02-22
Many thanks,
John
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo "<input type='checkbox' value = {$day->format('Y-m-d')}>" . $day->format('Y-m-d')."<br />";
}

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.

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