Hello i have this code:
$anno = 2020;
$settimana = 53;
$anno2 = 2021;
$settimana2 = 1;
for($i = 1; $i <= 7; $i++){
$giorno = date('d/m/Y', strtotime($anno."W".$settimana.$i));
echo $giorno."<br>";
}
echo "<br><hr><br>";
for($i = 1; $i <= 7; $i++){
$giorno2 = date('d/m/Y', strtotime($anno2."W".$settimana2.$i));
echo $giorno2."<br>";
}
This is the output of first loop:
28/12/2020
29/12/2020
30/12/2020
31/12/2020
01/01/2021
02/01/2021
03/01/2021
This is the output of the second loop
15/03/2021
22/03/2021
29/03/2021
05/04/2021
12/04/2021
19/04/2021
26/04/2021
But i wait this output
04/01/2021
05/01/2021
06/01/2021
07/01/2021
08/01/2021
09/01/2021
How can i resolve the problem?
You can try this function
Modify this function as your requirements.
Create array of whole week by running a loop from 1 to 6 days.
you will get your required result.
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$ret['week_start'] = $dto->format('Y-m-d');
$dto->modify('+6 days');
$ret['week_end'] = $dto->format('Y-m-d');
return $ret;
}
$week_array = getStartAndEndDate(1,2021);
print_r($week_array);
//output
Array
(
[week_start] => 2021-01-04
[week_end] => 2021-01-10
)
EDIT:
I checked your code and i think problem is with $settimana2 = 1; change this variable to $settimana2 = 01;
Related
What I can do if would like start my January month since 2016-01-01 not 2016-01-02.
https://3v4l.org/uRRfU
function countDaysForChoosenYears(int $year):int
{
$att = [];
for ($i = 1; $i <= 12; $i++) {
$att[] = cal_days_in_month(CAL_GREGORIAN, $i, $year);
}
$att = array_sum($att);
return $att;
}
$YearMonthDayStructure = [];
$Date = new DateTime('2016-01-01');
for ($i = 1; $i <= countDaysForChoosenYears(2016); $i++) {
$monthName = $Date->format('F');
$yearNumber = $Date->format('Y');
$YearMonthDayStructure[$yearNumber][$monthName][$Date->format('d')] = $Date->add(new DateInterval('P1D'))->format('Y-m-d');
}
print_r($YearMonthDayStructure);
The very first time you enter the loop you are adding P1D to the date. So your first date ends up being 2 Jan because you added P1D to 1 Jan on that line with the $Date->add. It's not like $x++ where the '++' happens after you use the variable. It performs the add and then gives the result, with the added P1D, to the $YearMonthDayStructure value.
I'm currenty looking into this tutorial to create a calendar. The only problem I have atm is that my months are in english instead of dutch. How can I change the output of 'july' to 'juli' ?
<?php
$vandaag = date("d"); // Current day
$maand = date("m"); // Current month
$jaar = date("Y"); // Current year
$dagen = cal_days_in_month(CAL_GREGORIAN,$maand,$jaar); // Days in current month
$vorigemaand = date("t", mktime(0,0,0,$maand-1,1,$jaar)); // Days in previous month
$begin = date("N", mktime(0,0,0,$maand,1,$jaar)); // Starting day of current month
$einde = date("N", mktime(0,0,0,$maand,$dagen,$jaar)); // Finishing day of current month
$vorigestart = $begin - 1; // Days of previous month in calander
$counter = 1;
$volgendeMaandCounter = 1;
if($begin > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $begin) < 0){
$date = (($vorigemaand - $vorigestart) + $counter);
$class = 'class="blur"';
}else if(($counter - $begin) >= $dagen){
$date = ($volgendeMaandCounter);
$volgendeMaandCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $begin + 1);
if($vandaag == $counter - $begin + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
Thanks in advance!
try with this code:
setlocale(LC_TIME, 'de_DE', 'deu_deu');
/* print test date string */
echo strftime("%A, %d. %B %Y");
I want to use PHP to populate an array starting with today's date and going several days into the future. When I tried the following below all of the columns contain "2013-11-18." I have been toying with it for 2 hours, but to no avail. What am I missing?
//Get "Day 0", today if undefined
if(isset($_GET['DAY0']) == TRUE){
$day0 = new DateTime($_GET['DAY0']);
} else {
$day0 = new DateTime('today');
}
// save day0 + 7 days into into dayArray
$dayArray[0] = $day0;
for($i=1; $i<8; $i++){
$day0->modify('+1 day');
$dayArray[i]= $day0;
}
echo "<tr>";
for ($i = 0; $i < 7; $i++) {
echo "<th>".$dayArray[i]->format('Y-m-d')."</th>";
}
echo "</tr>";
Objects are passed by reference. You are assigning multiple references to the same object in your array.
If you really need all the datetime objects in the array, you could do something like this
$interval = new DateInterval('P1D');
$start = new DateTime('today');
$dayArray = [clone $start];
for ($i = 1; $i < 8; $i++) {
$dayArray[] = clone $start->add($interval);
}
Or you could just store the formatted dates as already suggested.
$interval = new DateInterval('P1D');
$start = new DateTime('today');
$dayArray = [$start->format('Y-m-d')];
for ($i = 1; $i < 8; $i++) {
$dayArray[] = $start->add($interval)->format('Y-m-d');
}
Replace two of your $dayArray[i] with $dayArray[$i]
You could save timestamps:
// save day0 + 7 days into into dayArray
$dayArray[0] = $day0->format('U');
for($i=1; $i<8; $i++){
$day0->modify('+1 day');
$dayArray[$i] = $day0->format('U');
}
echo "<tr>";
for ($i = 0; $i < 7; $i++) {
echo "<th>".date('Y-m-d', $dayArray[$i])."</th>";
}
You can create a DatePeriod like so:
if(isset($_GET['DAY0']) == TRUE){
$day0 = new DateTime($_GET['DAY0']);
} else {
$day0 = new DateTime('today');
}
$enddate = new DateTime();
$period = new DatePeriod(
$day0,
new DateInterval('P1D'),
$enddate->add(new DateInterval('P7D'))
);
echo "<tr>";
foreach ($period as $datetime) {
echo "<th>".datetime->format('Y-m-d')."</th>";
}
echo "</tr>";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generate incrementing date strings
I have:
$start_date = '2012-09-03';
$number_days = 5;
I would like receive array with this dates:
$dates = array(
'2012-09-03',
'2012-09-04',
'2012-09-05',
'2012-09-06',
'2012-09-07'
);
What is the best way for this?
$start_date = '2012-09-03';
$dates[] = $start_date;
$number_days = 5;
for ($i=1; $i < $number_days; $i++) {
$dates[] = date('Y-m-d', strtotime("$start_date +$i days"));
}
http://php.net/manual/en/function.date-add.php
Loop over and add the dates you need.
Have you tried something like this
function get_days($start_date, $max){
$ts=strtotime($start_date);
$next_day_interval=24*60*60;
$arr=array();
$arr[]=$start_date;
for($i=1;$i<=$max;$i++){
$ts += $next_day_interval;
$arr[]=date('Y-m-d', $ts);
}
return $arr;
}
Just wrote it here so there might be some compile time errors but I hope you get the idea.
Here's what you're searching for (work also on PHP < 5.3)
<?php
$start_date = '2012-09-03';
$number_days = 5;
$stdate = date(strtotime($start_date));
$dates = array();
for($i = 0 ; $i < $number_days ; $i++) {
$dates[$i] = date('Y-m-d', $stdate) ;
$stdate += 24*60*60;
}
var_dump($dates);
?>
Try this.
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
$dates[0] = $start_date;
for($i=1;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
}
Modified Code from #vinay to print actual output
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dates = array();
$TS = strtotime($start_date);
for($i=0;$i<5;$i++)
{
$dates[$i] = date('Y-m-d', strtotime('+1 day', $TS));
$TS = strtotime($dates[$i]);
echo date('y-m-d',$TS).'<br>';
}
?>
Here is simple example with PHP 5.3 DateTime and DateInterval. This is clear solution. Note: PHP 5.2 supports DateTime, but not DateInterval. You may declare it in custom class in PHP 5.2, see here: DateInterval Definition.
<?php
$start_date = '2012-09-03';
$number_days = 5;
$dt = new DateTime($start_date);
$dates = array();
for($i = 0; $i < $number_days; $i++) {
$dates[] = $dt->format("Y-m-d");
$dt->add(new DateInterval("P1D"));
}
print_r($dates);
?>
I'm struggling with how to build the following:
result I need:
$months = array();
$months
(
month[0] = '2011-10-1'
month[1] = '2011-11-1'
month[2] = '2011-12-1'
month[3] = '2012-1-1'
)
from the following variables:
$date = '2011-10-1';
$numberOfMonths = 4;
Any help would be appreciated.
Thanks, guys... all of these solutions work. I accepted the one that works best for my usage.
You can do it using a simple loop and strtotime():
$date = '2011-10-1';
$numberOfMonths = 4;
$current = strtotime($date);
$months = array();
for ($i = 0; $i < $numberOfMonths; $i++) {
$months[$i] = date('Y-n-j', $current);
$current = strtotime('+1 month', $current);
}
$date = DateTime::createFromFormat('Y-m-j', '2011-10-1');
$months = array();
for ($m = 0; $m < $numberOfMonths; $m++) {
$next = $date->add(new DateInterval("P{$m}M"));
$months[] = $next->format('Y-m-j');
}
<?php
function getNextMonths($date, $numberOfMonths){
$timestamp_now = strtotime($date);
$months[] = date('Y-m-d', $timestamp_now);
for($i = 1;$i <= $numberOfMonths; $i++){
$months[] = date('Y-m-d', (strtotime($months[0].' +'.$i.' month')));
}
print_r($months);
}
getNextMonths('2011-10-1', 4);
Working demo
You could use split on date and then a for loop over the month. The trick is to use something like
$newMonth = ($month + $i) % 12 + 1;
(% is called modulo and does exactly what you want.)