PHP >> Date Period Foreach Won't run - php

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']);

Related

How to include the end date in a DatePeriod?

I am trying to get a Date range for all workdays this week. I have written the following code to do so.
Code
$begin = new DateTime('monday this week'); 2016-07-04
$end = clone $begin;
$end->modify('next friday'); // 2016-07-08
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
echo $date->format('Y-m-d')."<br />";
}
Output
2016-07-04
2016-07-05
2016-07-06
2016-07-07
In the output friday is missing. I can fix this by doing $end->modify('next saturday') but I was wondering why the last day of a DatePeriod is not included in the range.
The iterator seems to check the time as well as the date, it excludes the end element if the time in the endDate is less that or equal to the time in the start date.
So ensure the time of the end date is at least a second greater that that of the start date.
// this will default to a time of 00:00:00
$begin = new DateTime('monday this week'); //2016-07-04
$end = clone $begin;
// this will default to a time of 00:00:00
$end->modify('next friday'); // 2016-07-08
$end->setTime(0,0,1); // new line
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
echo $date->format('Y-m-d')."<br />";
}
Try this code
<?php
$begin = new DateTime('2016-07-04');
$end = clone $begin;
$end->modify('next friday'); // 2016-07-08
$end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach ($daterange as $date) {
echo $date->format('Y-m-d') . PHP_EOL;
}
In PHP 8.2 you can use DatePeriod::INCLUDE_END_DATE as constructor option!
https://www.php.net/manual/en/class.dateperiod.php#dateperiod.constants.include-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.

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);
}

Adding months to DateTime with DateInterval changes original date to match new date

I have this pretty simple code:
$start_date = new DateTime($post['start_date']);
$end_date = $start_date->add(new DateInterval('P6M'));
echo $start_date->getTimestamp(); // 1351836000
echo $end_date->getTimestamp(); // 1351836000
Of course, both end up as the same timestamp because adding the date interval affects the original $start_date. So how do I go about this so I can keep the original $start_date yet add 6 months to it in another variable?
I tried this with no luck:
$start_date = new DateTime($post['start_date']);
$start_date_actual = $start_date;
$end_date = $start_date_actual->add(new DateInterval('P6M'))->getTimestamp();
Variables hold references to objects, not the objects themselves. So assignment just gets you more variables pointing to the same object, not multiple copies of the object.
If you want a copy, use the clone keyword:
$end_date = clone $start_date;
$end_date->add(new DateInterval('P6M'));
You can take your pick:
$start_date = new DateTime($post['start_date']);
$end_date = new DateTime($post['start_date']);
$end_date->add(new DateInterval('P6M'));
or
$start_date = new DateTime($post['start_date']);
$end_date = clone $start_date;
$end_date->add(new DateInterval('P6M'));
Clone the start date before you modify it:
$start_date = new DateTime();
$end_date = clone $start_date;
$end_date->add(new DateInterval('P6M'));
echo $start_date->getTimestamp();
echo $end_date->getTimestamp();
See the manual for more information
http://php.net/manual/en/language.oop5.cloning.php
http://php.net/manual/en/language.oop5.references.php

Categories