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 />";
}
Related
I need to create a date-time with specific day each month between a start date and a end date.
Example :
Specific day : 04
Start : 2016-12-22
End : 2017-03-06
Output needed : 2016-01-04 ; 2016-02-04 ; 2016-03-04
How can I do that?
You can create a DatePeriod object and iterate over it to get all the dates you need:
$start = new DateTime('2016-12-22');
$end = new DateTime('2017-03-06');
// Compute the occurrence on the same month as $start
$first = new DateTime();
$first->setDate($start->format('Y'), $start->format('m'), 4);
// If it is in the past of $start then the first occurrence is on the next month
if ($first < $start) {
$first->add(new DateInterval('P1M'));
}
// Create a DatePeriod object that iterates between $first and $end
// in increments of 1 month
$period = new DatePeriod($first, new DateInterval('P1M'), $end);
// Run through the list, process each item
foreach ($period as $day) {
echo($day->format('Y-m-d')."\n");
}
I found it ! This following code is working for me :
$d1 = new DateTime("2016-12-22");
$d2 = new DateTime("2017-03-06");
$dd = '04';
if ($dd < $d1->format('d') ) {
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
} else {
$date = $d1->format('Y').'-'.$d1->format('m').'-'.$dd;
}
$date = new DateTime($date);
print_r($date);echo('<br />');
while ($date->add(new \DateInterval('P1M')) <= $d2){
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
$date = new DateTime($date);
print_r($date);echo('<br />');
}
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 have two dates
2016-06-22 , 2016-07-11
I need to print the date in numbers for example,
22,23,24,25,26,27,28,29,30,1,2,.....11
if the month is july to august it should print
22,23,24,25,26,27,28,29,30,31,1,2,.....11
according to month wise also in PHP.
Thank you.
This will work for you .. Look The DatePeriod class
A date period allows iteration over a set of dates and times, recurring at regular intervals, over a given period.
<?php
$begin = new DateTime( '2016-06-22' );
$end = new DateTime( '2016-07-11' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("d") . "<br>";
}
?>
LIVE EXAMPLE : CLICK HERE
You have to iterate over date between start and end date and print it in format d.
$fromDate = new DateTime('2016-06-22');
$toDate = new DateTime('2016-07-11');
$days = array();
while($fromDate <= $toDate) {
$days[] = $fromDate->format('d');
$fromDate->modify('tomorrow');
}
echo implode(',', $days);
Try:
function createDateRangeArray($strDateFrom,$strDateTo)
{
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('d',$iDateFrom));
}
}
return $aryRange;
}
$arr = createDateRangeArray("2016-06-22","2016-07-11");
echo implode(",",$arr);
in mysql
select date_format(my_date_column, '%d$) from my_table
in php
$date = new DateTime('2016-06-22');
echo $date->format('d');
echo the day number
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.
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);
}