$listData2 = [];
$start = new DateTime('2017-01-01');
$end = (new DateTime('2017-01-12'))->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $index => $period1)
{ echo $listData2[] = $period1->format("d/m/Y"); }
Hi, I found the below code to output my dates from 01/01/2017 until 12/01/2017 and it's working fine. I used the code above on PHP 5.5 However, when I copy this code to a different computer using PHP 5.3, there's lot of error. How do I change the above code so that it's compatible with PHP 5.3
You can fix this by not using [] for arrays and initializing your date object and then manipulating it. Will work down to 5.3.0 - Example
<?php
$listData2 = array();
$start = new DateTime('2017-01-01');
$end = new DateTime('2017-01-12');
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $index => $period1) {
echo $listData2[] = $period1->format("d/m/Y");
}
I tested your code in http://sandbox.onlinephpfunctions.com.
This (new DateTime('2017-01-12'))->modify('+1 day'); causing
syntax error, unexpected T_OBJECT_OPERATOR
Solution
Just move modify('+1 day').
<?php
$listData2 = array();
$start = new DateTime('2017-01-01');
$end = new DateTime('2017-01-12');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end->modify('+1 day'));
foreach ($period as $index => $period1)
{ echo $listData2[] = $period1->format("d/m/Y") . "<br/>"; }
Just test it here if you want: http://sandbox.onlinephpfunctions.com/code/32f0fed1fb0c89c7bd3dff0ed21f9178ecca
Related
I am trying to echo a variable that contains the last 30 days into tha label of a chart. I don't know what I'm missing.
> $today = new DateTime();
$begin = $today->sub(new DateInterval('P30D'));
$end = new DateTime();
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach ($daterange as $date) {
$d[] = $date->format("M j");
}
$labels = "'".implode("','",$d)."'";
The line from chart config
labels: [<? echo $labels; ?>],
This is the output: 'Feb 24','Feb 25','Feb 26','Feb 27','Feb 28','Mar 1','Mar 2','Mar 3','Mar 4','Mar 5','Mar 6','Mar 7','Mar 8','Mar 9','Mar 10','Mar 11','Mar 12','Mar 13','Mar 14','Mar 15','Mar 16','Mar 17','Mar 18','Mar 19','Mar 20','Mar 21','Mar 22','Mar 23','Mar 24','Mar 25','Mar 26'
I thought this was the correct version, but the chart doesn t work, it s blank.I don't know what I'm missing.
Thanks in advance
I want to loop start date and end date and generate dates by month. I use this code:
<?php
$start = (new DateTime($cust_data->date_sold));
$end = (new DateTime($cust_data->due_date));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo "<tr>";
echo "<td>".$dt->format("m-d-Y")."</td>";
echo "</tr>";
}
Example date:
$start = '02-05-2018'
$end = '08-05-2018'
Result:
02-05-2018
03-05-2018
04-05-2018
05-05-2018
06-05-2018
07-05-2018
I want it to be like this:
03-05-2018
04-05-2018
05-05-2018
06-05-2018
07-05-2018
08-05-2018
But I don't know how.
An approach that uses only native DateTime tools without having to parse the date again.
$start = (new DateTime($cust_data->date_sold))->modify('+1 day');
$end = (new DateTime($cust_data->due_date))->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("d-m-Y") . '<br>';
}
Hope This will help you.
$s1 = date('d-m-Y', strtotime('02-05-2018' . ' + 1 day')); // Added one day to start from 03-05-2018
$s2 = date('d-m-Y', strtotime('08-05-2018'.' + 1 day')); //Added one day to end with 08-05-2018
$start = new DateTime($s1);
$end = new DateTime($s2);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo "<pre>".$dt->format("d-m-Y");
}
I have Added code as per d-m-y format. you can change as you needed.
If you're using PHP
$start = new DateTime('1-1-2017');
$end = new DateTime('4-1-2017');
split date to 3
result :
array(
array('start'=> 1-1-2017,'end'=>2-1-2017),
array('start'=> 2-1-2017,'end'=>3-1-2017),
array('start'=> 3-1-2017,'end'=>4-1-2017)
);
thanks
Try this..!!
Use DateTime to iterate dates :
$start = new DateTime('1-1-2017');
$end = (new DateTime('4-1-2017'))->modify('+1 day');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("d-m-y") . "<br>\n";
}
This is my code
$from = '2014-10-01 00:00:00';
$to = '2015-05-31 23:30:00';
$start = new DateTime('#' . strtotime($from), new DateTimeZone('Asia/Dubai'));
$end = new DateTime('#' . strtotime($to), new DateTimeZone('Asia/Dubai'));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$count = 0;
foreach ($period as $dt) {
echo $dt->format('Y-M').'<br>';
}
exit;
I need to get the month's starting date and month's ending date for a certain period. So, I use the DatePeriod.
But, this only shows this output.
2014-Sep
2014-Oct
2014-Nov
2014-Dec
2015-Jan
2015-Mar
2015-Apr
2015-May
And somehow the month February is missed.
Can anyone help me on this ?
Just remove strtotime and # from the code and it'll work fine. As there is no need to make a timestamp of your date value
$from = '2014-10-01 00:00:00';
$to = '2015-05-31 23:30:00';
$start = new DateTime($from, new DateTimeZone('Asia/Dubai'));
$end = new DateTime($to, new DateTimeZone('Asia/Dubai'));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$count = 0;
foreach ($period as $dt) {
echo $dt->format('Y-M').'<br>';
}
$start = new DateTime('2013-08-16');
$interval = new DateInterval('P1D');
$end = new DateTime('2013-08-20');
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}
I found the above code to get all dates between two static dates.
What I would like is to get the dates between multiple $start $end pairs. The scenario is to associate the variables ($start, &end) with fields from a db in order to make multiple pairs and get the between dates of these pairs into an array.
Is it possible?
Thanks!
just write the code in a function like so
function find_dates_between( $start_date, $end_date) {
$start = new DateTime($start_date);
$interval = new DateInterval('P1D');
$end = new DateTime($end_date);
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}
}
After that just call the function
find_dates_between( $start_date, $end_date);
where $start_date and $end_date are extracted from you DB