Display Range of Date in Specific Month - php

I want to display date range for specific month in php as shown below:
Suppose i select month January and year 2015 then code should display :
Date 01/01/2015 to 10/01/2015
10/01/2015 to 20/01/2015
21/01/2015 to 31/01/2015
Kindly help me out to resolve this issue as soon as possible.
thanks in advance..

try this code
$list=array();
$month = 11;
$year = 2014;
for($d=1; $d<=31; $d++)
{
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month)
$list[]=date('Y-m-d-D', $time);
}
echo "<pre>";
print_r($list);
echo "</br>";
echo $list[0] . " to " . $list[9] ."</br>";
echo $list[10]. " to " .$list[19] ."</br>";
echo $list[20]. " to " . end($list) ."</br>";
echo "</pre>";
output
2014-11-01-Sat to 2014-11-10-Mon
2014-11-11-Tue to 2014-11-20-Thu
2014-11-21-Fri to 2014-11-30-Sun

try this
function dateRange( $first, $last, $step = '+10 days', $format = 'Y/m/d' ) {
$dates = array();
$current = strtotime( $first );
$last = strtotime( $last );
while( $current <= $last ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
$dates = dateRange( '2010/07/26', '2010/08/05') ;
foreach($dates as $date)
{
echo $date;
}

Related

How to get the start date and end date when the year number and month number are known in php

I have the year number (ex: 2018) and the month number (Ex:04). I want to get the starting date and the ending date for that month. like
2018-04-01 and 2018-04-30
try this
$year = 2018;
$month = 4;
$date_start = date('Y-m-d', strtotime(date($year.'-'.$month).' first day of this month'));
$date_end = date('Y-m-d', strtotime(date($year.'-'.$month).'last day of this month'));
echo $date_start . ' and ' . $date_end;
$months_array = $this->getting_particular_months_dates($start_day_of_year, $end_day_of_year);
$arra1=array();
foreach ($months_array as $K => $v)
{
$year=explode( '-',$v['month'])[1];
$month=explode( '-',$v['month'])[0];
$arra1[]=$this->get_month_dates((int)$year,(int)$month);
}
return $arra1;
public function getting_particular_months_dates($year_start_date, $year_end_date)
{
$month_array = array();
$date1 = $year_start_date;
$date2 = $year_end_date;
$output = [];
$time = strtotime($date1);
$last = date('m-Y', strtotime($date2));
do {
$month = date('m-Y', $time);
$total = date('t', $time);
$output[] = [
'month' => $month,
'total' => $total,
];
$time = strtotime('+1 month', $time);
} while ($month != $last);
$month_array = $output;
return $month_array;
}
public function get_month_dates($year, $month)
{
$date_start = date('Y-m-d', strtotime(date($year . '-' . $month) . ' first day of this month'));
$date_end = date('Y-m-d', strtotime(date($year . '-' . $month) . 'last day of this month'));
$a = array('first_day' => $date_start, 'last_day' => $date_end);
return $a;
}

how to display months in descending order starting with current month in php [duplicate]

I'm trying to list all months between two dates.
For example; start date is: 2010-12-02 and last date is: 2012-05-06
I want to list something like this:
2010-12
2011-01
2011-02
2011-03
2011-04
.
.
.
2012-04
2012-05
This is what I have tried and it is not working at all:
$year_min = 2010;
$year_max = 2012;
$month_min = 12;
$month_max = 5;
for($y=$year_min; $y<=$year_max; $y++)
{
for($m=$month_min; $m<=$month_max; $m++)
{
$period[] = $y.$m;
}
}
PHP 5.3
$start = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end = new DateTime('2012-05-06');
$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("Y-m") . "<br>\n";
}
See it in action
PHP 5.4 or newer
$start = (new DateTime('2010-12-02'))->modify('first day of this month');
$end = (new DateTime('2012-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
The part where we modify the start and end dates to the first of the month is important. If we didn't, and the current day higher then the last day in February (i.e. 28 in non-leap years, 29 in leap years) this would skip February.
function getMonthsInRange($startDate, $endDate)
{
$months = array();
while (strtotime($startDate) <= strtotime($endDate)) {
$months[] = array(
'year' => date('Y', strtotime($startDate)),
'month' => date('m', strtotime($startDate)),
);
// Set date to 1 so that new month is returned as the month changes.
$startDate = date('01 M Y', strtotime($startDate . '+ 1 month'));
}
return $months;
}
You must make a difference between two months of the same year and two months of different years.
$year_min = substr($row['contractStart'], 0, 4);
$year_max = substr($row['contractEnd'], 0, 4);
$month_min = substr($row['contractStart'], 5, 2);
$month_min = substr($row['contractEnd'], 5, 2);
$period = array();
try {
if ($year_min > $year_max)
throw new Exception();
else if ($year_min == $year_max)
if ($month_min > $month_max)
throw new Exception();
for ($month = $month_min; $month <= $month_max; $month++) {
$period[] = $month . '-' . $year;
}
else {
for ($month = $month_min; $month <= 12; $month++) {
$period[] = $month . '-' . $year_min;
}
for ($year = $year_min + 1; $year < $year_max; $year++) {
for ($month = $month_min; $month <= $month_max; $month++) {
$period[] = $month . '-' . $year;
}
}
for ($month = 1; $month <= $month_max; $month++) {
$period[] = $month . '-' . $year_max;
}
}
implode("<br />\r\n", $period);
}
catch (Exception $e) {
echo 'Start date occurs after end date.'
}
That's for the hard way. Now there is a quick and easy way that is already given as an answer which I recommend you to choose.
This was my solution since DateTime is not available in my server environment.
$a = "2007-01-01";
$b = "2008-02-15";
$i = date("Ym", strtotime($a));
while($i <= date("Ym", strtotime($b))){
echo $i."\n";
if(substr($i, 4, 2) == "12")
$i = (date("Y", strtotime($i."01")) + 1)."01";
else
$i++;
}
Try it out: http://3v4l.org/BZOmb
In Laravel,
$period = \Carbon\CarbonPeriod::create('2017-06-28', '1 month', '2019-06-01');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
October 2021 Update
If you have dates selected by the user, here's a solution
$from = date('Y-m-d', strtotime($_POST['from']));
$to = date('Y-m-d', strtotime($_POST['to']));
$counter = 1;
$max_date = strtotime($to);
$current_date = strtotime($from);
$dates = [];
$months = [];
$loop = true;
while($loop) {
if(strtotime(date('Y-m-d',$current_date)." +".$counter."days") >= $max_date) $loop = false;
else {
$current_date = strtotime(date('Y-m-d', $current_date)." +".$counter."days");
$date = date('Y-m-d', $current_date);
$dates[] = $date;
$months[] = date('Y-m', $current_date);
$counter++;
}
}
$months = array_unique($months);
echo '<pre>';
print_r($dates);
echo '<br>';
print_r($months);
echo '</pre>';

Get the same specific date on each month of the whole year

I am working on a "cut-off" date and I need to set it on every month. Say, I set the cut-off date to August 25 for this year, then it should be September 25, October 25 and so on till the year ends.
Here's the code I have:
$now = "2015-08-25";
$nextDate = getCutoffDate($now);
echo $nextDate;
function getCutoffDate($start_date)
{
$date_array = explode("-",$start_date); // split the array
// var_dump($date_array);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
/*if (date('n', $now)==12)
{
return date("Y-m-d",strtotime(date("Y-m-d", $start_date) . "+1 month"));
}*/
if (date("d") <= $day) {
$billMonth = date_format(date_create(), 'm');
}
else{
$billMonth = date_format(date_modify(date_create(), 'first day of next month'), 'm');
}
// echo date("d").' '. $billMonth.'<br>';
$billMonthDays = cal_days_in_month(CAL_GREGORIAN, ($billMonth), date("Y"));
// echo $billMonthDays.'<br>';
if ($billMonthDays > $day) {
$billDay = $day;
} else {
$billDay = $billMonthDays;
}
}
I got this from here: http://www.midwesternmac.com/blogs/jeff-geerling/php-calculating-monthly
It returns the same date for the next month only, but how do I get the specific date of EACH month of the current year? Kindly leave your thoughts. Still a newbie here, sorry.
In that case, this should be enough:
<?php
for($i=8; $i<=12; $i++) {
echo sprintf('25-%02d-2015', $i) . '<br>';
}
but if you need more flexible way:
<?php
$date = new DateTime('25-08-2015');
function getCutoffDate($date) {
$days = cal_days_in_month(CAL_GREGORIAN, $date->format('n'), $date->format('Y'));
$date->add(new DateInterval('P' . $days .'D'));
return $date;
}
for($i = 0; $i < 5; $i++) {
$date = getCutoffDate($date);
echo $date->format('d-m-Y') . '<br>';
}
This should print:
25-09-2015
25-10-2015
25-11-2015
25-12-2015
25-01-2016

How to loop through an associative array in PHP?

Lets assume I have the following:
$times = array();
for($month = $current_month; $month <= $end_month; $month++) {
$first_minute = mktime(0, 0, 0, $month, 1);
$last_minute = mktime(23, 59, 0, $month, date('t', $first_minute));
$times[$month] = array($first_minute, $last_minute);
}
I now need to loop through all the $months but I am not sure how I can achieve this.
I have tried foreach() but that requires I enter a value for $month. We are assuming the value is not clear.
Do you mean you need to loop through $times?
A simple foreach will do...
foreach ($times as $month => $mins) {
// $month = the month
// $mins[0] = first minute
// $mins[1] = last minute
}
Can try using foreach() like this
foreach($times as $month=>$val){
echo 'Month: ' . $month . ' ';
echo 'First Minute: ' . $val[0] . ' ';
echo 'last Minute: ' . $val[1];
echo '<br />';
}
Perhaps you should use Datetime instead?
$end_month = 12;
$times = [];
$oneMonth = new DateInterval('P1M');
$dt = new Datetime('midnight first day of'); // 2014-12-01T00:00:00+00:00
while($dt->format('m') <= $end_month) {
$firstSecond = $dt->getTimestamp();
$dt->add($oneMonth); // 2015-01-01T00:00:00+00:00
$lastSecond = $dt->getTimestamp() -1;
$times[$dt->format('m')] = [$firstSecond, $lastSecond];
}
foreach ($times as $month => $seconds) {
// ...
}
But you could only work with Datetime object:
while($dt->format('m') <= $end_month) {
$times[] = clone $dt;
$dt->add($oneMonth);
}
foreach ($times as $month) {
echo $month->format('m'), // 12
$month->getTimestamp();
}

Get day names between two date in PHP

How to get name of days between two dates in PHP?
Input:
Start Date: 01-01-2013
End date: 05-01-2013
Output:
Tuesday
Wednesday
Thursday
Friday
Saturday
Tried code
$from_date ='01-01-2013';
$to_date ='05-01-2013';
$number_of_days = count_days(strtotime($from_date),strtotime($to_date));
for($i = 1; $i<=$number_of_days; $i++)
{
$day = Date('l',mktime(0,0,0,date('m'),date('d')+$i,date('y')));
echo "<br>".$day;
}
function count_days( $a, $b )
{
$gd_a = getdate( $a );
$gd_b = getdate( $b );
$a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
$b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
return round( abs( $a_new - $b_new ) / 86400 );
}
I saw the post Finding date of particular day between two dates PHP
But I don't got my result
Please help me
Use the DateTime class, it will be much more simple:
$from_date ='01-01-2013';
$to_date ='05-01-2013';
$from_date = new DateTime($from_date);
$to_date = new DateTime($to_date);
for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
echo $date->format('l') . "\n";
}
$from_date ='01-01-2013';
$to_date ='05-01-2013';
$start = strtotime($from_date);
$end = strtotime($to_date);
$day = (24*60*60);
for($i=$start; $i<= $end; $i+=86400)
echo date('l', $i);
<?php
function printDays($from, $to) {
$from_date=strtotime($from);
$to_date=strtotime($to);
$current=$from_date;
while($current<=$to_date) {
$days[]=date('l', $current);
$current=$current+86400;
}
foreach($days as $key=> $day) {
echo $day."\n";
}
}
$from_date ='01-01-2013';
$to_date ='05-01-2013';
printDays($from_date, $to_date);
?>
Loop through every day between the given dates (inclusive) and then add the corresponding day in an array, using date function. Print out the array and tada! you're done!

Categories