Function for selecting next Day If the last day is weekend - php

I am trying to create a function that checks the range of date. If the last date is an weekend it will select the next day. Suppose, Today is 1 March 2016. If someone add 5 with it, it will make the result 7 March 2016. Because 6 March is Weekend. I have written this code. But it is not working and I cannot find where is the problem. The code is as below:
<?php
$date = "2016-02-29";
$numOfDays = 8;
$futureDate = strtolower(date("l",strtotime($date." +".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate != $$w1 || $futureDate != $w2){
$finalDate = date("Y-m-d",strtotime($futureDate));
}
else{$finalDate = date("Y-m-d",strtotime($futureDate ."+1 day"));}
echo $finalDate;
?>

Check this:
<?php
$date = "2016-02-29";
$numOfDays = 11;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
echo $finalDate1;
?>

March 6th, 2016 is a Sunday, but you're checking for Friday and Saturday, you need to check for Saturday and Sunday instead, for the weekend. Also, your variable names need to match, and for Saturday you'll need to add two days to get the next weekday.
<?php
$date = "2016-02-29";
$numOfDays = 8;
$day = 86400;//one day is 86400 seconds
$time = strtotime($futureDate + $numOfDays * $day);//converts $futureDate to a UNIX timestamp
$futureDate = strtolower(date("l", $time));
$saturday = "saturday";
$sunday = "sunday";
if($futureDate == $saturday){
$finalDate = date("Y-m-d", $time + 2 * $day);
}
else if($futureDate == $sunday){
$finalDate = date("Y-m-d", $time + $day);
}
else{
$finalDate = date("Y-m-d", $time);
}
echo($finalDate);
?>

#Fakhruddin Ujjainwala
I have changed in your code and it works now perfect. Thanks. the code is now as below:
<?php
$date = "2016-02-29";
$numOfDays = 4;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
else if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
else{
$finalDate1 = date("Y-m-d",strtotime($futureDate1));
}
echo $finalDate1;
?>

Related

Disable Specific Date

So I want to Disable Specific date in Months, This is my Function in Controller called "jurnal.php"
function jurnal_harian_form() {
setlocale(LC_ALL,"US");
echo "<h3 id='title'>Jurnal Harian :: Daftar SKP :: Form Jurnal Harian</h3>";
$_GET['nip'] = ($_SESSION['NIP']);
$data_profil_pegawai = $this->jurnal_mdl->get_data_statistik_Jabatan();
$harilibur = $this->jurnal_mdl->GetHariLibur();
$currentYear = date("Y");
$currentDay = date("d");
$month = strftime("%B");
$min = date('Y-m-d', strtotime("$month $currentYear + 14 days"));
$now = date('Y-m-d');
$r =0;
for ($i=1; $i < 6 ; $i++) {
$gg = date('Y-m-d',strtotime("$i $month $currentYear"));
$issun = date('w',strtotime("$gg"));
if ($issun == '6' || $issun == '0') {
$min = date('Y-m-d', strtotime("$min +1 days"));
$r++;
}
}
$hasilmin = '';
$hasilmax = '';
if($now >= $min){
$hasilmin = date('Y-m-d', strtotime("first day of $month $currentYear"));
if ($r == 1) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +1 month +6 days"));
} elseif($r == 2) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +14 days"));
}else{
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +1 month +5 days"));
}
}else if($now == $min){
$hasilmin = date('Y-m-d', strtotime("first day of $month $currentYear"));
if ($r == 1) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +5 days"));
} elseif($r == 2) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +6 days"));
}else{
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +4 days"));
}
}else{
$hasilmin = date('Y-m-d', strtotime("first day of $month $currentYear "));
if ($r == 1) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +5 days"));
}
elseif($r == 2 && $currentDay <= 15) {
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +14 days"));
}
elseif($r == 2 && $currentDay >= 15) {
$hasilmax = date('Y-m-d', strtotime("$now +14 days"));
}
else{
$hasilmax = date('Y-m-d', strtotime("$month $currentYear +4 days"));
}
}
$data = array(
"html_store" => $this->get_store_form_tambah_jurnal(),
"pegawai" => $data_profil_pegawai,
"harilibur" => $harilibur,
"minvalue" => $hasilmin,
"maxvalue" => $hasilmax
);
$this->load->view('jurnal/grid_jurnal_harian2', $data);
}
I want to make ,when the month is still on the 1st to 15th, the 16th to the end of the month is disabled, but if it's already on the 16th, the 1st to 15th is disabled. how to make this happend?
If you want to limit days in date field of the Front end (extjs), you can use setDisabledDates method of the Date Field.
Ex: To disable the 5th and 6th of October, use: datefield.setDisabledDates(["10/05/2003", "10/06/2003"])
For more detail, view the link

PHP print dates without sunday

I have this form with this input field
<input type="text" name="txt_datetimein" class="form-control datetime">
<input type="text" name="txt_datetimeout" class="form-control datetime">
<input type="text" name="txt_lenght" class="form-control">
I enter the first date and the second date and the length the length i precise the number of repetition
than i click next and i have all days without sunday
example if i put 5 in the length and datetimein 01-04-2017 8:00:00 and datetimeout is 01-04-2017 5:00:00
the output will be like that
Date IN Date Out Day
01-04-2017 8:00:00 01-04-2017 5:00:00 Saturday
03-04-2017 8:00:00 03-04-2017 5:00:00 Monday
04-04-2017 8:00:00 04-04-2017 5:00:00 Tuesday
05-04-2017 8:00:00 05-04-2017 5:00:00 Wednesday
06-04-2017 8:00:00 06-04-2017 5:00:00 Thursday
07-04-2017 8:00:00 07-04-2017 5:00:00 Friday
this my code but it's print all day
<?php
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date)) == 'Sunday';
if ($is_sunday) {
$day = date('l', strtotime("+1+$i days",strtotime($datetimein)));
}
else {
$day = date('l', strtotime("+$i days",strtotime($datetimein)));
}
}
?>
How Can i solve my Problem ??!!
try this below code
$datetimein = "01-04-2017 8:00:00";
$datetimeout = "01-04-2017 5:00:00";
$lenght = 20;
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date));
if($is_sunday == "Sunday")
{
$i=$i+1;
}
$day = date('l', strtotime("$i days",strtotime($datetimein)));
echo $day."<br>";
}
Use DateTime and all those objects. Simpler and cleaner :-)
<?php
$begin = new DateTime();
$end = clone $begin;
$end = $end->modify('+14 day');
$interval = new DateInterval('P1D');
$range = new DatePeriod($begin, $interval ,$end);
foreach($range as $date) {
if ($date->format('N') !== 7) {
echo $date->format('Y-m-d'), '<br>';
}
}
Date format N is the day of week as a number where 7 === Sunday.
Here is you programe
$datetimein = '01-04-2017 8:00:00';
$datetimeout= '01-04-2017 5:00:00';
$lenght = 5;
$i=0;
$days = array();
$dt = strtotime($datetimein);
while($i < $lenght){
if(date('D',$dt)!='Sun'){
$days[] = date('Y-m-d D',$dt);
$i++;
}
$dt = $dt+24*3600;
}
print_r($days);
In this line $days[] = date('Y-m-d D',$dt); change the format or save both in and out time whatever you want. $days will have you expected dates.
It looks like you have mis-spelled the variable length.
Check this.
<?php
$lenght = 5;
$in_temp = 0;
$out_temp = 0;
for($i=0;$i<=$lenght;$i++) {
$in = strtotime($datetimein) + $in_temp;
$out = strtotime($datetimeout) + $out_temp;
$date = date('m/d/Y H:i:s', strtotime("+$i days", $in));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", $out));
$is_sunday = strtolower(date('l', strtotime($date))) == 'sunday';
if ($is_sunday) {
$in_temp += 86400 ; // Adding 1 day in seconds.
$day = date('l', (strtotime($date)+$in_temp));
}
else {
$day = date('l', (strtotime($date)));
}
echo $day."\n";
}
?>

I want to update my date every wednesday at 12:00 A.M. in the dropdown in php

I want to update my date every wednesday at 12:00 A.M. in the dropdown in php and remains unchanged till next wednesday.
Following is my code:
$now = time(); // current timestamp
$today = date("w", $now); // "w" returns the weekday (number)
$wednesday = 3; // 5th day of the week (sunday = 0)
if ($today == $wednesday) {
$ts = $now; // today is wednesday, don't change the timestamp
$daysLeft = 0; // no days left!
} else {
$daysLeft = $wednesday-$today; // get the left days
$ts = $now + 84600 * $daysLeft; // now + seconds of one day * days left
}
?>
<h1>
Forecast for <?php echo date("Y-m-d", $ts) ?>
</h1>
In it date remains unchanged on wednesday which is correct and then change quickly as soon as thursday begins. Although I want it to remain same till next wednesday.
I think you are over-complicating the issue.
<?php
$today = time();
$nextWed = strtotime('next wednesday');
if(date('D', $today) === 'Wed') {
$ts = date('Y-m-d', $today);
} else {
$ts = date('Y-m-d', $nextWed);
}
echo '<h1>Forecast for '.$ts.'</h1>';
?>
What's happening?
Get timestamp for today
Get timestamp for next Wednesday
If today is Wednesday, $ts = today's date
If today is NOT Wednesday, $ts = next Wednesday's date
Echo your results
EDIT
<?php
$now = time();
$today = date('Y-m-d', $now);
if(date('D', $now) === 'Wed') { $nextWed = strtotime($today); }
if(date('D', $now) === 'Thu') { $nextWed = strtotime("$today - 1 days"); }
if(date('D', $now) === 'Fri') { $nextWed = strtotime("$today - 2 days"); }
if(date('D', $now) === 'Sat') { $nextWed = strtotime("$today - 3 days"); }
if(date('D', $now) === 'Sun') { $nextWed = strtotime("$today - 4 days"); }
if(date('D', $now) === 'Mon') { $nextWed = strtotime("$today - 5 days"); }
if(date('D', $now) === 'Tue') { $nextWed = strtotime("$today - 6 days"); }
$ts = date('Y-m-d', $nextWed);
echo '<h1>Forecast for '.$ts.'</h1>'
?>

Detect Saturday and Sunday and add x amount of days to Monday

I want it to automatically add days until Monday if someone choose Friday.
Imagine $leavefrom is 3-1-2014 which is Thursday, and $leaveto is 3-2-2014 is Friday. $totaldays are calculated based on the date. Therefore it is 2 days.
<?php
$x = 0;
$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);
while ($x < $totaldays) {
$tomorrow = date('l', strtotime($date1 ."+1 days"));
//$tomorrow = date("m-d-Y", strtotime( $date1 ."+1 days" ));
$getday = date('D', strtotime($tomorrow));
$x++;
if ($getday == "Sunday" || $getday = "Saturday") {
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}
echo $tomorrow;
?>
If you're just trying to skip weekends just check to see if $date2 is on a weekend, if so, skip ahead to the next Monday.
$date2 = DateTime::CreateFromFormat('n-j-Y', $leaveto);
if (in_array($date2->format('l'), array('Sunday', 'Saturday'))) {
$date2->modify('next Monday');
}
echo $date2->format("m/d/Y");
Try changing the if ($getday == "Sunday" || $getday = "Saturday") into a while, instead, and get rid of the last $tomorrow = .... Something like this:
<?php
$x = 0;
$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);
while ($x < $totaldays) {
$tomorrow = date('l', strtotime($date1 ."+1 days"));
$x++;
$getday = date('D', strtotime($tomorrow));
while ($getday == "Sunday" || $getday = "Saturday") {
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
$getday = date('D', strtotime($tomorrow));
}
}
echo $tomorrow;
?>
I found solution after 3 hours of head bang on the wall for being stupid, below is my code:
while ($daysloop <= $totaldays) {
$tomorrow1 = date("m/d/Y", strtotime( $tomorrow1 ."+1 days" ));
$dayofweek = date('w', strtotime($tomorrow1));
if ($dayofweek == 0 || $dayofweek == 6) {
$weekends = $weekends + 1;
}
$daysloop++;
}
if ($totaldays == 0) {
$totaldays = $totaldays - $weekends + 1;
}
else {
$totaldays = $totaldays - $weekends;
}

getting remaining days excluding weekends

i just want to know how to get the remaining days excluding the weekends. i tried subtracting two dates but i cant seem to find any solutions on removing weekends. well this is my code:
$date_registered = date('Y-m-d');
$date_planned = $_POST['start_date'];
$dueDate = $date_registered;
$numDays = 3;
$counter = 1;
while ($counter <= $numDays) {
$dueDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($dueDate)) . " +1 day"));
$dayOfTheWeek = date("l",strtotime($dueDate));
if ($dayOfTheWeek == "Saturday" || $dayOfTheWeek == "Sunday") {
continue;
}else {
$counter++;
}
}
echo $date_registered.'<br>';
echo $date_planned.'<br>';
//echo $dueDate;
$remaining_days = strtotime($date_registered) - strtotime($date_planned);
echo $remaining_days/86400;
i dont have any idea how to exclude the weekends .i hope you could help me.
Try this one
$date = date('Y-m-d');
$total_days_left = (strtotime($end_date) - strtotime($current_date)) / (60 * 60 * 24);
while (strtotime($date) <= strtotime($end_date)) {
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if($day=='Sat' || $day=='Sun') {
$count++ ;
}
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
Let me know if you face any issue. Count will provide the number of week end days falling between these two days.From that you can count remaining day easily.
$total_day_left_excluding_weekends = $total_days_left - $count;
Refer date and strtotime on official PHP site.
$time = $sTime = START_TIMESTAMP;
$eTime = END_TIMESTAMP;
$count = 0;
while(date('w', $time) != 0) {
$time += 86400;
}
while($time < $eTime) {
$count++;
$time += 7 * 86400;
}

Categories