Want to alert Effective Date, till it meets the condition.
Code:-
$effective_date = '2020/09/03';
$last_date = '2020/12/03';
$last_date = new DateTime($last_date);
$effective_date = new DateTime($effective_date);
do{
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
} while($effective_date > $last_date);
It alerts only one time, but my expectation is it should be alerted 3 times
It alerts only one time because your while condition is not correct.
Notice that the do { ... } while (condition) loop will run at least once and continue as long as the condition is true, not until it becomes true.
Change while($effective_date > $last_date) to while($effective_date < $last_date):
$effective_date = '2020/09/03';
$last_date = '2020/12/03';
$last_date = new DateTime($last_date);
$effective_date = new DateTime($effective_date);
do{
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
} while($effective_date < $last_date);
Ouput:
<script>alert('2020/09/03');</script><script>alert('2020/10/03');</script><script>alert('2020/11/03');</script>
Update
If you want the condition to be checked before the statements inside the loop are executed, use the while loop instead:
while ($effective_date < $last_date) {
$edate = $effective_date->format('Y/m/d');
echo "<script>alert('".$edate."');</script>";
$nxtdate = date('Y-m-d', strtotime("+1 months", strtotime($edate)));
$effective_date = new DateTime($nxtdate);
}
Related
I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));
Hi I've created a while loop for dates. I want to increase the month my 1 every time. However it isn't working and instead it increments like this :
2009/06/01
2009/07/01
2009/09/01
2009/12/01
2010/04/01
2010/09/01
which isn't correct. I dont understand why it won't increment by 1 month. Any help would be much appreciated
<?php
$startdate = "2009/06/01";
$enddate = "2009/12/31";
$start = strtotime($startdate);
$end = strtotime($enddate);
$f = 0;
$t = 6;
$d = 0;
$currentdate = $start;
while($f < $t )
{
$cur_date = date('Y/m/d', $currentdate);
$currentdate = strtotime($f . ' month', $currentdate);
echo $cur_date . "<br />";
//echo $f . "<br />";
$f = $f + 1;
}
?>
It won't work because you're not adding 1 month each time.... you're adding 1 month in the first iteration, 2 in the second, 3 in the third, etc. because you're increasing the value of $f every iteration.
$begin = new DateTime('2009-06-01');
$end = new DateTime('2009-12-31');
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
foreach($period as $dt) {
var_dump($dt->format( "Y-m-d" ));
}
Consider following code:
$today = date("d/m/Y");
$start_date = '20/11/2014';
$time1 = strtotime($start_date1);
$start_date1 = date('d/m/Y',$time1);
$end_date = '20/01/2015';
$time2 = strtotime($end_date1);
$end_date1 = date('d/m/Y',$time2);
if( $start_date1 <= $today && $end_date1 >= $today)
echo "yes";
else
echo 'no';
Even though $today is in between those start and end date, I get "no" in return. What might be the problem here? I just wanna check if today is in between those dates. Start date and end date are saved as string in DB.
Try this:
<?php
$now = new DateTime();
$startdate = new DateTime("2014-11-20");
$enddate = new DateTime("2015-01-20");
if($startdate <= $now && $now <= $enddate) {
echo "Yes";
}else{
echo "No";
}
?>
I'm trying to get the difference between two linux times. I want to show the difference in months. I am getting one Linux time from the database and comparing it against time();.
$sql_sms_transactions = "SELECT MAX(`sendtime`) FROM `sms_transaction` where customer_id='$customer_id'";
$result_sms_transactions = mysql_query($sql_sms_transactions);
while($row2=mysql_fetch_assoc($result_sms_transactions))
{
$status = $row2['MAX(`sendtime`)'];
//print $status;
$month = date('m', $status); // 1-12
$year = date('YW', $status);
//print $month;
//print $year;
$current=time();
$monthcurrent = date('m', $current); // 1-12
$yearcurrent = date('YW', $current);
//print $monthcurrent;
//print $yearcurrent;
}
Using DateTime and DateInterval objects, this should work:
$date1 = new DateTime();
$date1->setTimestamp($timestamp1);
$date2 = new DateTime();
$date2->setTimestamp($timestamp2);
$diff = $date1->diff($date2);
// Result of type DateInterval
echo ($diff->y*12) + $diff->m
From what I understand of your code, you have to do this beforehand:
$timestamp1 = time();
$timestamp2 = $status;
How can I limit this date or print this date from the star_date to end_date?
ex.
$start_date="2011-05-15";//june 15 2011
$end_date="2011-07-30";//july -7,2011
The result should be.
$dates[]="2011-05-15";
$dates[]="2011-05-16";
$dates[]="2011-05-17";
$dates[]="2011-05-18";
$dates[]="....";
$dates[]="....";
$dates[]="....";
$dates[]="....";
until it reaches.
$dates[]="2011-07-30";
I would suggest take the start date as an object and keep adding 1 day (http://www.php.net/manual/en/datetime.add.php) in a loop until you reach the end date.
<?php
$start_date = new DateTime('2011-05-15');
$end_date = new DateTime('2011-07-30');
while($end_date > $start_date)
{
echo $start_date->format('Y-m-d') . "\n";
$start_date->add(new DateInterval('P1D'));
}
?>
The above code has not been tested.
This could do it ...
$start_date = strtotime('2011-05-15'); //june 15 2011
$end_date = strtotime('2011-07-30'); //july -7,2011
$dates = array();
for ($i=$start_date; $i<=$end_date; $i+=86400) {
$dates[] = date('Y-m-d',$i);
}
$start_date="2011-05-15";
$end_date="2011-07-30";
$date=$start_date;
while (strtotime($new_date) != strtotime($end_date))
{
echo $new_date=date("Y-m-d",strtotime("+1 day", strtotime($date)))."<br>";
$dates[]=$new_date;
$date=$new_date;
}
And another....
date_default_timezone_set('America/Los_Angeles');
$startDate='2011-05-15';
$endDate='2011-07-30';
$t1=strtotime($startDate);
$days=(strtotime($endDate)-$t1)/86400;
for($i=0;$i<=$days;$i++) $dates[]=date('Y-m-d',$t1+($i*86400));
print_r($dates);
And for the 'not very efficient but will do in most cases oneliner':
for($t=strtotime($startDate);$t<=strtotime($endDate);$t+=86400) $dates[]=date('Y-m-d',$t);
And one for those who know, the last for this mornings exercises:
$dates=array_map(create_function('$t','return date("Y-m-d",$t);'),range(strtotime($startDate),strtotime($endDate),86400));
This works:
$start_date = "2011-05-15";
$end_date = "2011-07-07";
$dates = array();
$stop = strtotime($end_date);
for($i = strtotime($start_date); $i <= $stop; $i += 86400)
$dates[] = date('Y-m-d', $i);
PS. I changed your July date to 07-07 to match up with your comment.
$start_date = strtotime('2011-05-15');
$end_date = strtotime('2011-07-30');
$dates = array();
for ($i = $start_date; $i<=$end_date; $i+= (strtotime('+1 day') - strtotime('now'))) {
$dates[] = date('Y-m-d',$i);
}