Increment for loop by date - php

I am trying to increment date by new date. but it is not showing any result.
$ed = strtotime($endDate);
for ($i = $ed; $i <= strtotime($today); $i = $ed) {
$toArray['d'] = $ed;
$startDate = date('y-m-d', strtotime("+1 day", strtotime($endDate)));
$endDate = date('y-m-d', strtotime("+13 day", strtotime($startDate)));
$ed = strtotime($endDate);
}
It is not working.
Whats wrong..?

$endDate='2017-06-21';
$endDate='2017-05-05';
$ed = strtotime($endDate);
for($i = $ed; $i >= strtotime($today); $i = $ed ){
$toArray['d'] = $ed;
$startDate = date('y-m-d', strtotime("+1 day", strtotime($endDate)));
$endDate = date('y-m-d', strtotime("+13 day", strtotime($startDate)));
$ed = strtotime($endDate);
}

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);

Loop through start date to end date

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.
}

PHP Date Selector

I've made the following date selector in PHP for a timetable but when i use it, it starts at the start of PHP time (1970). How do I make it show the current date?
//date selector
$weekStart = 1;
$date = date('Y').'-'. date('m').'-'. date('d');
$timestamp = strtotime($date);
$dayOfWeek = date('N', $timestamp);
echo("<br><br><form action='' name='timetable' method='Post'><div style='position:relative;left:40%;'><h4>Select Week Beginning</h4></div><div style='float:middle;position:relative;left:39%;'><select onchange='submit();' name='date'>");
for ($i = 0; $i <= 10; $i++) {
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart + ( $i * 7), date('Y', $timestamp));
if(date('d-m-Y', $startDate) == $_POST['date']){
$selected = "selected='selected'";
}
else{
$selected = "";
};
echo("<option ".$selected." >". date('d-m-Y', $startDate)."</option>");
}
echo('</select></form></div><br>');
echo("<table style='border:1px solid;'>");
$weekStart = 0;
if(isset($_POST['date'])){
$timestamp = strtotime($_POST['date']);
}else{
$timestamp = strtotime($date);
};
for ($i = 1; $i <= 7; $i++) {
$dayrow = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart + $i , date('Y', $timestamp));
echo("<td style='text-align:left;width:800px;margin-top:10px;margin-bottom:10px;padding-left:10px;'><h4>".date('D j-m-Y', $dayrow)."</h4></td>");
echo("<td >");
Thanks In Advance

Why won't this for loop work for iterating through unix time?

I am trying to populate a select list with time.
I want to create the select list so that it starts from the starting date and then ends six months later.
I've created this for loop for now but it doesn't work:
$dateSelectList = '';
$startDate = $c->getStartDate(92);
$endDate = intval( strtotime('+6 month', $startdate) );
$i = 1;
$tempDate = 0;
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$i.'" value="'.$date.'">'.$date.'</option>';
$i++;
}
$dateSelectList .= '</select>';
I think it's the last field in the for loop but I don't know how to get around it.
I've changed it to $date = strtotime('+1 day', $date) and it works now.
Thanks a lot !
In each iteration, you're resetting the date to the start date plus one day. I.e., you're just using the same date over and over each iteration:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $startdate))
Change your for loop so that it keeps adding on to $date instead:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
There are plenty of solutions. One of them may be:
Code
$startdate = time(); // today;
$enddate = strtotime('+6 months', $startdate);
while ($startdate <= $enddate) {
echo date('Y-m-d', $startdate) . "<br/>";
$startdate = strtotime('+1 day', $startdate);
}
Output
2012-03-26
2012-03-27
2012-03-28
2012-03-29
2012-03-30
2012-03-31
2012-04-01
...
2012-09-24
2012-09-25
2012-09-26
Now, modify code and create your selector as you like.
Change first line to
$year = 2012;
$month = 3;
$day = 26;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
and create your custom $startdate.
Complete selector code
$year = 2012;
$month = 2;
$day = 3;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
echo $doc;
Output
More elegant solution is to put it all into function like this
function createSelector($day, $month, $year) {
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
return $doc;
}
and call it this way
$selectorCode = createSelector(26, 3, 2012);
echo $selectorCode;
Cheers!
The problem is, indeed, with this bit of code: $date = strtotime('+1 day', $startdate) ...
$startdate is never being changed, therefore, $date is never being changed. You'll want something more like $date = strtotime('+1 day', $date) in order for the loop to work properly.

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