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.
Related
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
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 />";
}
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);
}
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
I'm having two DateTime-objects:
$start = new DateTime('first thursday of June 2012');
$end = new DateTime('2012-12-31');
I need a DatePeriod that contains all first Thursdays of the months between this two dates. When using
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
This only adds 1 month without respecting the condition "first thursday".
Also something like this does not work:
$interval = DateInterval::createFromDateString('1 month first thursday');
$period = new DatePeriod($start, $interval, $end);
Does anyone know how I can achieve this?
I've struggled with this the last two hours - as soon as I posted it here I got the solution.
I only had to change the DateInterval string to "first thursday of next month".
$start = new DateTime('first thursday of June 2012');
$end = new DateTime('2012-12-31');
$interval = DateInterval::createFromDateString('first thursday of next month');
$period = new DatePeriod($start, $interval, $end);
Works! DateTime rocks ;-)