php or mysql list dates between range? - php

I need to generate a list of dates (with either php or mysql or both) where i have a start and end date specified? For example if the start date is 2012-03-31 and the end date is 2012-04-05 how can i generate a list like this?
2012-03-31
2012-04-01
2012-04-02
2012-04-03
2012-04-04
2012-04-05
I have a mysql table with a start and end date but i need the full list of dates.

Something like this should do it:
//Get start date and end date from database
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
$date_list = array($start_date);
$current_time = $start_time;
while($current_time < $end_time) {
//Add one day
$current_time += 86400;
$date_list[] = date('Y-m-d',$current_time);
}
//Finally add end date to list, array contains all dates in order
$date_list[] = $end_date;
Basically, convert the dates to timestamps and add a day on each loop.

Using PHP's DateTime library:
<?php
$start_str = '2012-03-31';
$end_str = '2012-04-05';
$start = new DateTime($start_str);
$end = new DateTime($end_str . ' +1 day'); // note that the end date is excluded from a DatePeriod
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
echo $day->format('Y-m-d'), "\n";
}
Source

Try this:
<?php
// Set the start and current date
$start = $date = '2012-03-31';
// Set the end date
$end = '2012-04-05';
// Set the initial increment value
$i = 0;
// The array to store the dates
$dates = array();
// While the current date is not the end, and while the start is not later than the end, add the next day to the array
while ($date != $end && $start <= $end)
{
$dates[] = $date = date('Y-m-d', strtotime($start . ' + ' . $i++ . ' day'));
}
// Output the list of dates
print_r($dates);

Related

PHP Dates getting a start date and end date for each period

Okay I'm trying to generate the end of the billing period from one date; my question is if I have a variable $billing_period = 02/28/2016 and the billing periods are on every 14th and 28th of each month. from this one variable how can I generate the end date of the period when they have different days appart depending on the start date?
What's confusing to me is that if the date is the 28th it has either 15 or 16days apart from the 14th which is the start of the next billing period. And if the date is the 14th then it has 14 days apart from the 28th. Thanks for any help
EDIT
- The image here shows the date which is selected which is the 02/14/2016 how can I echo the next billing date which would be 02/28/2016 from just the start date
This is my code for the array and getting the start date.
<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
$currentdate = date('y-m-d');
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
}
?>
Hope I understood well.
What you are trying to do is determine which is your billing period depending on the date you previously selected.
So, you select a date ($date).
Then you need to know the selected day
$timestamp = strtotime($date);
$day = date('D', $timestamp);
Now that you have your day you can make the comparations.
$datetime = new DateTime($date);
if ($day == 14 ){ // if you select 14th then your billing is on the 28th
$billing = $datetime->modify($date->format('Y-m-28'));
}else{ // if you didn't select 14th, then you select 28, and you add one month and set the day in 14th.
$next_month = $datetime->modify('+1 month');
$billing = $next_month->modify($date->format('Y-m-14'));
}
Build an array with the billing dates, get the current key and increment it to get the next value. Simple example with strings:
$dates = ['02/28/2016', '03/14/2016', '03/28/2016', ...];
$key = array_search( '03/14/2016', $dates );
$nextDate = $dates[$key + 1]; // 03/28/2016
Is this what you want to get?
Here the code to get you the next date, just in case you want to do it as a one off.
$date = new DateTime();
$next = $date->format('14/m/Y');
if ($date->format('d') >= 14 && $date->format('d') < 28) {
$next = $date->format('28/m/Y');
}
if ($date->format('d') >= 28) {
$date->modify('+1 month');
$next = $date->format('14/m/Y');
}
echo $next;
Here's the code turned into the function which you can call whenever you need it, it'll return a DateTime() object so you can output the date in whatever format you need at the time, or perform further comparisons/modifications should you need to down the line.
function nextBillingDate($date, $format = 'd/m/Y') {
$date = DateTime::createFromFormat($format, $date);
$next = $date->format('14/m/Y');
if ($date->format('d') >= 14 && $date->format('d') <= 28) {
$next = $date->format('28/m/Y');
}
if ($date->format('d') >= 28) {
$date->modify('+1 month');
$next = $date->format('14/m/Y');
}
return DateTime::createFromFormat('d/m/Y', $next);
}
Example use:
$date = nextBillingDate('28/02/2016');
echo $date->format('d/m/Y');
Outputs:
14/03/2016
Hope it helps.

Want to add dates for week

I am using this code to add a week to a date:
$date1 = "2009-10-11";
$d = new DateTime($date1);
$d->modify( '+1 week' );
echo $d->format( 'Y m d' ), "\n";
It works fine good but want to add this functionality:
$startDate = "2009-10-11";
$endDate = "2010-01-20";
And want to create an array that holds ALL the +1 weeks IN BETWEEN these dates. How can i do this?
Here is one way of doing it:
$startDate = "2009-10-11";
$endDate = "2010-01-20";
$dates = array();
$temp = strtotime($startDate);
do {
$dates[] = date("Y-m-d", $temp);
$temp = strtotime("+1 week", $temp);
} while ($temp < strtotime($endDate));
print_r($dates);
You can see a demo here
Dates can be converted to timestamps. Timestamps are great for being compared because they basically just integers.
What I would do as a quick'n'dirty solution is to convert both your dates to timestamps and then design a loop like this (pseudo-code) :
timestamp = start_timestamp
WHILE timestamp < end_timestamp
timestamp = timestamp + 1 week
dates[] = timestamp
END WHILE

Print dates between range of dates

I want to know how to print all the dates between range of dates given in PHP 5.2
I do not want to call function for this task.
This should do the job.
<?php
$start = '2013/01/01'; //start date
$end = '2013/01/30'; //end date
$dates = array();
$start = $current = strtotime($start);
$end = strtotime($end);
while ($current <= $end) {
$dates[] = date('Y/m/d', $current);
$current = strtotime('+1 days', $current);
}
//now $dates hold an array of all the dates within that date range
print_r($dates);
?>

How to list dates in yyyy_mm format with PHP using a loop?

What's the cleanest way to use a loop in PHP to list dates in the following way?
2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11
The key elements here:
Should be as simple as possible - I would prefer one for loop instead of two.
Should list this month's date as the first date, and should stop at a fixed point (2009-11)
Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)
Had to make a few tweaks to the solution:
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = date('Y').'-'.date('m').'-01';
// End date
$end_date = '2009-1-1';
while (strtotime($date) >= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
echo substr($date,0,7);
echo "\n";
}
Maybe this little code does the thing? :
more complicated situations.
<?php
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
The credit goes to: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/
This is what im guessing your asking for cause it doesnt really make sense......
$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;
//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
//Second for loop to loop threw months
for($o=$startmonth; $o<=12; $o++) {
//If statement to check and throw stop when at limits
if($i == $endyear && $o <= $endmonth)
echo $i."_".$o."<br/>";
else
break;
}
}
Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7
PHP 5.3 introduces some great improvements to date/time processing in PHP. For example, the first day of, DateInterval and DatePeriod being used below.
$start = new DateTime('first day of this month');
$end = new DateTime('2009-11-01');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y_m') . PHP_EOL;
}

How to get start and end date of months of a given range of date in php

I am using following codes to display start and end dates of current month.
function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}
$date_start = firstOfMonth();
$date_end = lastOfMonth();
echo $date_start;
echo $date_end;
Question: How to get start and end dates of all months in a range of date given
For Eg:
function daterange($startdate,$enddate)
{
...
...
...
}
Expected result be array of start and end dates of each month between date range of $startdate and $enddate.
Help me how to do this....
<?php
//Function to return out start and end dates of all months in a date range given
function rent_range($start_date, $end_date)
{
$start_date = date("m/d/Y", strtotime($start_date));
$end_date = date("m/d/Y", strtotime($end_date));
$start = strtotime($start_date);
$end = strtotime($end_date);
$month = $start;
$months[] = date('Y-m', $start);
while($month < $end) {
$month = strtotime("+1 month", $month);
$months[] = date('Y-m', $month);
}
foreach($months as $mon)
{
$mon_arr = explode( "-", $mon);
$y = $mon_arr[0];
$m = $mon_arr[1];
$start_dates_arr[] = date("m/d/Y", strtotime($m.'/01/'.$y.' 00:00:00'));
$end_dates_arr[] = date("m/d/Y", strtotime('-1 minute', strtotime('+1 month',strtotime($m.'/01/'.$y.' 00:00:00'))));
}
//to remove first month in start date and add our start date as first date
array_shift($start_dates_arr);
array_pop($start_dates_arr);
array_unshift($start_dates_arr, $start_date);
//To remove last month in end date and add our end date as last date
array_pop($end_dates_arr);
array_pop($end_dates_arr);
array_push($end_dates_arr, $end_date);
$result['start_dates'] = $start_dates_arr;
$result['end_dates'] = $end_dates_arr;
return $result;
}
$start_date = '2011-07-29';
$end_date = '2012-03-31';
$res = rent_range($start_date, $end_date);
echo "<pre>";
print_r($res);
echo "</pre>";
?>
My Above Function will give the month range display of dates within a given range.
This function would be useful for monthly rent calculation as indian tradition.
It may help some one else....
Have a look at date function and scroll to format character t which gives you the number of days. Start date will always be 1 :-)
Doing this in a loop for the number of months between the two dates and storing the values in an array is up to you
function firstAndLast($d=''){
$d = $d?$d:time();
$f = mktime(0,0,0,date("n",$d),1,date("Y",$d));
$l = mktime(0,0,0,date("n",$d),date("t",$d),date("Y",$d));
return array($f,$l);
}
list($first,$last) = firstAndLast();
echo date("d/m/Y",$first)." ($first) - ".date("d/m/Y",$last)." ($last)";
You can pass a timestamp to the function or leave it blank and it will pick up the current time

Categories