Looping dates by month Codeigniter - php

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.

Related

How to get Months between two dates

I'm trying to get All the months between two dates.
for e.g if the user posted an article on 21-10-2012 and today date is 5-12-2017. Now i want to get all the month and Year between this period like shown below
10-2012
11-2012
01-2013
02-2014
03-2015
04-2015
05-2015
06-2015
07-2015 // and so on
.......
.......
.......
12-2017 // Till Today Date
Till Now i was only able to calculate the difference.
$article_date= date("d-m-Y", $article['date']);
$year = date("Y");
$month = date("m");
$day = date("d");
$date1 = new DateTime($article_date);
$date2 = new DateTime("$day-$month-$year");
$diff = $date1->diff($date2);
echo (($diff->format('%y') * 12) + $diff->format('%m')) . " full months difference";
How can i get all the months?
Use this code
$start = new DateTime('2012-10-21');
$start->modify('first day of this month');
$end = new DateTime('2017-12-05');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("m-Y") . "<br>\n";
}
function list_months($date_from,$date_to, $return_format){
$arr_months = array();
$a = new \DateTime($date_from);
$x = new \DateTime($date_to);
$start = $a->modify('first day of this month');
$end = $x->modify('first day of next month');
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$arr_months[] = $dt->format($return_format);
}
return $arr_months ;
}
Example: $new_list = list_months('11-10-2012','11-10-2017', 'm-Y');

how to split date start end to array?

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

Get years and month between two dates

I am trying this code:
$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$interval = new DateInterval('P1Y'); // 1 Year interval
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval
$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
echo $dts->format( "m" );
}
I'm getting output like this:
year:2015
Month:08,09,...,01
In this output I'm not getting year:2016 and month:02. I want my output like this:
year:2015,2016
Month:08,09,...,01,02
How can I get this?
And if my end date is "2016-08-20"
than i m getting year:2016 but not getting month :08
Note that my start date and end date is not fixed.
As mentioned before, the end date is not included
You may also modify the end date like that
$end = $end->modify( '+1 day +1 year' );
See php's DateTime modify for more info.
Update
If end year is more than 1 year bigger than the begin date then adding a year will indeed return a year more than what we want.
A solution is to check and add the year only if is needed like that:
if($begin > (new DateTime( $end_date))->modify('-1 year')) {
$end->modify( '+1 year' );
}
$end->modify('+1 day');
You can use the following code to achieve this:
<?php
$start = (new DateTime('2015-12-02'))->modify('first day of this month');
$end = (new DateTime('2016-05-06'))->modify('first day of this month');
//For Year
$interval = DateInterval::createFromDateString('1 year');
$period = new DatePeriod($start, $interval, $end);
echo 'Year: ';
foreach ($period as $dt)
{
echo $dt->format("Y") . ",";
}
//For Month
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
echo '<br/>Month: ';
foreach ($period as $dt)
{
echo $dt->format("m") . ",";
}
?>
Alternatively, you can display it nicely in Y-m format by the following code:
$start = (new DateTime('2015-12-02'))->modify('first day of this month');
$end = (new DateTime('2016-05-06'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("Y-m") . "<br>\n";
}
since the year 2016 and the month 02 is not completed you are not getting it.
try this
$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1Y1M'));
$interval = new DateInterval('P1Y'); // 1 Year interval
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval
$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
echo $dts->format( "m" );
}

Weird PHP issue - DatePeriod does not show February while iterating through months.

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

Return all dates between multiple dates in a single array

$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

Categories