Loop through start date to end date - php

I have two columns name 'start' and 'end' of type date. I want to loop through the date from start to end and insert some data in a table according to month.
I tried the following code but nothing inserted in my table.
Here is my code:
$session_info = $this->db->get_where('session', array('componentId' => $session_id))->row_array();
$start = strtotime($session_info['start']);
$end = strtotime($session_info['end']);
$fee_classwise = $this->db->get_where('fee_conf', array('class_id' => $class_id))->result_array();
foreach ($fee_classwise as $row) {
$feeInfo = $this->db->get_where('item', array('componentId' => $row['item_id']))->row_array();
if($feeInfo['category3']=='ONCE') {
$dataFee['studentFeeName'] = $feeInfo['itemName'];
$dataFee['studentId'] = $student_id;
$dataFee['sessionId'] = $running_year;
$dataFee['itemId'] = $feeInfo['componentId'];
$dataFee['amount'] = $feeInfo['salePrice'];
$dataFee['month'] = date('F', $start);
$dataFee['year'] = date('Y', $end);
$this->db->insert('student_feeconfig', $dataFee);
}
if($feeInfo['category3']=='SESSION') {
$session_start = $start;
$session_end = $end;
while($session_start < $session_end) {
$dataFee['studentFeeName'] = $feeInfo['itemName'].'-'.date('M', $session_start);
$dataFee['studentId'] = $student_id;
$dataFee['sessionId'] = $session_id;
$dataFee['itemId'] = $feeInfo['componentId'];
$dataFee['amount'] = $feeInfo['salePrice'];
$dataFee['month'] = date('F', $session_start);
$dataFee['year'] = date('Y', $session_start);
$this->db->insert('student_feeconfig', $dataFee);
$session_start = strtotime('+4 month', $session_start);
}
}
if($feeInfo['category3']=='MONTHLY') {
$month_start = $start;
$month_end = $end;
while($month_start < $month_end) {
$dataFee['studentFeeName'] = $feeInfo['itemName'] .'-'. date('M', $month_start);
$dataFee['studentId'] = $student_id;
$dataFee['sessionId'] = $session_id;
$dataFee['itemId'] = $feeInfo['componentId'];
$dataFee['amount'] = $feeInfo['salePrice'];
$dataFee['month'] = date('F', $month_start);
$dataFee['year'] = date('Y', $month_start);
$this->db->insert('student_feeconfig', $dataFee);
$month_start = strtotime('+1 month', $month_start);
}
}
}

$start = strtotime($start);
$end = strtotime($end);
$currentdate = $start;
while($currentdate <= $end)
{
$cur_date = date('Y-m-d', $currentdate);
$currentdate = strtotime('+1 days', $currentdate);
//do what you want here
}
this is simple example you can try, good luck

$startDate = new DateTime('2016-12-01');
$interval = new DateInterval('P1D'); // One day
$endData = new DateTime('2016-12-31');
$period = new DatePeriod($startDate, $interval, $endData);
foreach ($period as $dt)
{
echo $dt->format('d-m-y');
echo "<br>";
// Do whatever you want to do.
}

Related

Inserting dates within an array with PHP

I am getting the starting and ending dates from a form.
I need to put within an array all the date between the former two, including themselves.
I'm using a normal for loop and, at the same time, printing the result, to verify.
Everything appear to be alright.
But when I print_r the very array, I get only a series of equal dates. Which are all the same: last date + 1.
This is the code:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-03-22");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
$diffDays = $diff->days;
echo $diffDays;
$dates = array();
$addDay = $date1;
for ($i = 0; $i < $diffDays; $i++) {
$dates[$i] = $addDay;
date_add($addDay, date_interval_create_from_date_string("1 day"));
echo "array: " . $i . " : " . date_format($dates[$i], 'Y-m-d');
}
print_r($dates);
PHP code demo
<?php
$dates = array();
$datetime1 = new DateTime("2013-03-15");
$datetime2 = new DateTime("2013-03-22");
$interval = $datetime1->diff($datetime2);
$days = (int) $interval->format('%R%a');
$currentTimestamp = $datetime1->getTimestamp();
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
for ($x = 0; $x < $days; $x++)
{
$currentTimestamp = strtotime("+1 day", $currentTimestamp);
$dates[] = date("Y-m-d H:i:s", $currentTimestamp);
}
print_r($dates);
I would do it that way
$startDate = new \DateTime("2017-03-15");
$endDate = new \DateTime("2017-03-22");
$dates = [];
$stop = false;
$date = $startDate;
while(!$stop){
$dates[] = $date->format('Y-m-d'); // or $dates[] = $date->format('Y-m-d H:i:s')
$date->modify('+1 day');
if($date->getTimestamp() > $endDate->getTimestamp()){
$stop = true;
}
}
print_r($dates);

how to get sunday date between two date

I try this
<?php
$startdate = '2016-07-15';
$enddate = '2016-07-17';
$sundays = [];
$startweek=date("W",strtotime($startdate));
$endweek=date("W",strtotime($enddate));
$year=date("Y",strtotime($startdate));
for($i=$startweek;$i<=$endweek;$i++) {
$result=$this->getWeek($i,$year);
if($result>$startdate && $result<$enddate) {
$sundays[] = $result;
}
}
print_r($sundays);
public function getWeek($week, $year)
{
$dto = new \DateTime();
$result = $dto->setISODate($year, $week, 0)->format('Y-m-d');
return $result;
}
?>
this return blank array. but in between two dates 2016-07-17 is Sunday.
I get output as 2016-07-17
I refer this here
But in this link return output as no of sunday not date.
Give this a try:
$startDate = new DateTime('2016-07-15');
$endDate = new DateTime('2016-07-17');
$sundays = array();
while ($startDate <= $endDate) {
if ($startDate->format('w') == 0) {
$sundays[] = $startDate->format('Y-m-d');
}
$startDate->modify('+1 day');
}
var_dump($sundays);
If you want later to use the DateTime objects instead of the formatted date, then you must use DateTimeImmutable for the $startDate variable:
$startDate = new DateTimeImmutable('2016-07-15');
$endDate = new DateTimeImmutable('2016-07-17');
$sundays = array();
while ($startDate <= $endDate) {
if ($startDate->format('w') == 0) {
$sundays[] = $startDate;
}
$startDate->modify('+1 day');
}
var_dump($sundays);
function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$dateArr = array();
do
{
if(date("w", $startDate) != $weekdayNumber)
{
$startDate += (24 * 3600); // add 1 day
}
} while(date("w", $startDate) != $weekdayNumber);
while($startDate <= $endDate)
{
$dateArr[] = date('Y-m-d', $startDate);
$startDate += (7 * 24 * 3600); // add 7 days
}
return($dateArr);
}
$dateArr = getDateForSpecificDayBetweenDates('2010-01-01', '2010-12-31', 0);
print "<pre>";
print_r($dateArr);
Try out this code..
Try this
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$sundays = [];
while ($start->getTimestamp() != $end->getTimestamp()) {
if ($start->format('w') == 0) {
$sundays[] = $start->format('Y-m-d');
}
$start->add('+1 DAY');
}
This will return you all sundays between two dates.
$startdate = '2016-05-1';
$enddate = '2016-05-20';
function getSundays($start, $end) {
$timestamp1 = strtotime($start);
$timestamp2 = strtotime($end);
$sundays = array();
$oneDay = 60*60*24;
for($i = $timestamp1; $i <= $timestamp2; $i += $oneDay) {
$day = date('N', $i);
// If sunday
if($day == 7) {
// Save sunday in format YYYY-MM-DD, if you need just timestamp
// save only $i
$sundays[] = date('Y-m-d', $i);
// Since we know it is sunday, we can simply skip
// next 6 days so we get right to next sunday
$i += 6 * $oneDay;
}
}
return $sundays;
}
var_dump(getSundays($startdate, $enddate));
Use Carbon
$arrayOfDate = [];
$startDate = Carbon::parse($startDate)->modify('this sunday');
$endDate = Carbon::parse($endDate);
for ($date = $startDate; $date->lte($endDate); $date->addWeek()) {
$arrayOfDate[] = $date->format('Y-m-d');
}
return $arrayOfDate;

PHP loop - add days to date

I have the following code. It works to add days (daysBetween) to a date (startDate). However, I want to REPEAT it until it reaches the end date. How can I do this??
$startDate = "2009-10-11";
$endDate = "2010-01-20";
$daysBetween = 10;
function addDayswithdate($date,$days){
$date = strtotime("+".$days." days", strtotime($date));
return date("Y-m-d", $date);
}
$date = addDayswithdate($startDate,$daysBetween);
You can use following function;
<?php
$startDate = "2009-10-11";
$endDate = "2010-01-20";
$daysBetween = 10;
$finalResult = array();
function addDayswithdate($date,$days, $endDate, &$finalResult){
$tempDate = strtotime($date);
$tempDate += 3600*24*$days;
if ($tempDate < strtotime($endDate)) {
$finalResult[] = date("Y-m-d", $tempDate);
addDayswithdate(date("Y-m-d", $tempDate), $days, $endDate, $finalResult);
} else {
return true;
}
}
addDayswithdate($startDate,$daysBetween, $endDate, $finalResult);
var_dump($finalResult);
Here is a working demo: Demo
function addDayswithdate($from, $to, $interval){
$result = array();
while(true)
{
$dateTemp = strtotime("+".$interval." days", strtotime($from));
if(strtotime($dateTemp) > strtotime($to))
break;
$result[] = date("Y-m-d", $dateTemp);
$interval += $interval;
}
return $result;
}

PHP: List of days between two dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 4 years ago.
Is there an easy way to get a list of days between two dates in PHP?
I would like to have something like this in the end:
(pseudocode)
date1 = 29/08/2013
date2 = 03/09/2013
resultArray = functionReturnDates(date1, date2);
and the resulting array would contain:
resultArray[0] = 29/08/2013
resultArray[1] = 30/08/2013
resultArray[2] = 31/08/2013
resultArray[3] = 01/09/2013
resultArray[4] = 02/09/2013
resultArray[5] = 03/09/2013
for example.
$date1 = '29/08/2013';
$date2 = '03/09/2013';
function returnDates($fromdate, $todate) {
$fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
$todate = \DateTime::createFromFormat('d/m/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}
$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo $date->format('d/m/Y'), PHP_EOL;
}
function DatePeriod_start_end($begin,$end){
$begin = new DateTime($begin);
$end = new DateTime($end.' +1 day');
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
$dates[] = $date->format("Y-m-d");
}
return $dates;
}
dunno if this is at all practical, but it works pretty straight-forward
$end = '2013-08-29';
$start = '2013-08-25';
$datediff = strtotime($end) - strtotime($start);
$datediff = floor($datediff/(60*60*24));
for($i = 0; $i < $datediff + 1; $i++){
echo date("Y-m-d", strtotime($start . ' + ' . $i . 'day')) . "<br>";
}
Try this:
function daysBetween($start, $end)
$dates = array();
while($start <= $end)
{
array_push(
$dates,
date(
'dS M Y',
$start
)
);
$start += 86400;
}
return $dates;
}
$start = strtotime('2009-10-20');
$end = strtotime('2009-10-25');
var_dump(daysBetween($start,$end));
$datearray = array();
$date = $date1;
$days = ceil(abs($date2 - $date1) / 86400) + 1;//no of days
for($i = 1;$i <= $days; $i++){
array_push($datearray,$date);
$date = $date+86400;
}
foreach($datearray as $days){
echo date('Y-m-d, $days);
}

php loop by date of the month

Provided the code stated below, the output are (see below). My question is why after 2009/11/01 it follow's by 2009/11/30 and 2009/12/30 instead of 2009/12/01. From 2009/06/01 ~ 2009/11/01 there is no problem.
output
2009/06/01
2009/07/01
2009/08/01
2009/09/01
2009/10/01
2009/11/01
2009/11/30
2009/12/30
my code
<?php
$startdate = "2009/06/01";
$enddate = "2009/12/31";
$start = strtotime($startdate);
$end = strtotime($enddate);
$currentdate = $start;
while($currentdate < $end)
{
$cur_date = date('Y/m/d',$currentdate);
$month = date('m', $currentdate);
$year = date('Y', $currentdate);
$monthLength = daysOfMonth($month, $year);
$currentdate += $monthLength;
echo $cur_date . "<br />";
}
function daysOfMonth($month, $year)
{
return (86400 * date("t", strtotime($year."-".$month."-01")));
}
?>
<?php
$startdate = "2009/06/01";
$enddate = "2009/12/31";
$start = strtotime($startdate);
$end = strtotime($enddate);
$currentdate = $start;
while($currentdate < $end)
{
$cur_date = date('Y/m/d', $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
echo $cur_date . "<br />";
}
?>

Categories