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
Related
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;
?>
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;
}
I'm generating dates using my code I want to exclude sunday and saturday please check my code here
for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date"));
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week"));
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}
$n = count($weekfrom);
for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}
you can do like this.
$getDate = date('l', strtotime($date));
if ($getDate != 'Saturday' AND $getDate != 'Sunday') {
......
}
if that date not Saturday or Sunday, then process the thing.
Just add this to beginning of your loop:
if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;
Like this:
for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {
if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); //Returns the date of monday in week
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week")); //Returns the date of sunday in week
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}
$n = count($weekfrom);
for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}
I used:
strtotime(sprintf('+%d weekday', 3));
if now is 15 sep(Wednesday), the example returns: 20 sep(exclude weekends)
If I have 2 dates, 21/05/2010 and 23/05/2010, how can I find out if 22/05/2006 7:16 AM exists in between them?
I am using the following code to calculate the min/max date and will then SELECT ALL records in the table that are clear to update them.
$today = date('l');
if($today == 'Wednesday'){
$min = date('d/m/Y', strtotime('0 days'));
$max = date('d/m/Y', strtotime('+6 days'));
}else if($today == 'Thursday'){
$min = date('d/m/Y', strtotime('-1 days'));
$max = date('d/m/Y', strtotime('+5 days'));
}else if($today == 'Friday'){
$min = date('d/m/Y', strtotime('-2 days'));
$max = date('d/m/Y', strtotime('+4 days'));
}else if($today == 'Saturday'){
$min = date('d/m/Y', strtotime('-3 days'));
$max = date('d/m/Y', strtotime('+3 days'));
}else if($today == 'Sunday'){
$min = date('d/m/Y', strtotime('-4 days'));
$max = date('d/m/Y', strtotime('+2 days'));
}else if($today == 'Monday'){
$min = date('d/m/Y', strtotime('-5 days'));
$max = date('d/m/Y', strtotime('+1 days'));
}else if($today == 'Tuesday'){
$min = date('d/m/Y', strtotime('-6 days'));
$max = date('d/m/Y', strtotime('0 days'));
}
DateTime::diff
Create a DateTime::diff between 21/05/2010 and 22/05/2006 7:16 AM, as well as a DateTime::diff between 23/05/2010 and 22/05/2006 7:16 AM.
Then check that the first DateTime::diff is > 0, and the second is < 0
Update : used Datetime::createFromFormat, which is a php5.3 method
Update2 : Tested code sample. Produces expected output.
<?php
$datetime_lower = DateTime::createFromFormat('d/m/Y', '21/05/2010');
$datetime_upper = DateTime::createFromFormat('d/m/Y', '23/05/2010');
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', '22/05/2006 7:16 AM');
var_dump($datetime_lower < $datetime_compare);
var_dump($datetime_upper > $datetime_compare);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
echo " + date is between";
} else {
echo " date is not between";
}
Also, there is a procedural date_diff function
Use strtotime
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$datefind = strtotime($datefind);
if ($datefind >= $date1 && $datefind <= $date2)
Explode your dates and use the parts in a mktime() to get their timestamp values. Then it's a simple matter of checking if your timestamp is larger or smaller than the others.
http://php.net/manual/en/function.mktime.php
Pseudocode:
$parts= explode("/", "21/05/2010");
$timestamp1= mktime($parts[0], $parts[1], ...);
if($timestamp1 < $timestamp2...) {
print "timestamp1 is older then timestamp2";
}
Can anyone think of a better way of writing this out in a loop and getting the same result?
$today = date('l');
if($today == 'Wednesday'){
$min = date('l-m-d-y');
$max = date('l-m-d-y', strtotime('+4 days'));
}else if($today == 'Thursday'){
$min = date('l-m-d-y', strtotime('-1 days'));
$max = date('l-m-d-y', strtotime('+3 days'));
}else if($today == 'Friday'){
$min = date('l-m-d-y', strtotime('-2 days'));
$max = date('l-m-d-y', strtotime('+2 days'));
}else if($today == 'Saturday'){
$min = date('l-m-d-y', strtotime('-2 days'));
$max = date('l-m-d-y', strtotime('+1 days'));
}else if($today == 'Sunday'){
$min = date('l-m-d-y', strtotime('-3 days'));
$max = date('l-m-d-y');
}
echo $min . ' - ' . $max;
I assumed you wanted -3 in the min on Saturday and -4 on Sunday. Anyway, this is the idea:
$weekday = date("w");
if ($weekday == 0)
$weekday = 7;
if ($weekday >= 3) {
$min = date('l-m-d-y',
strtotime(($weekday==3?"+0":(3-$weekday))." days");
$max = date('l-m-d-y',
strtotime("+".(7-$weekday)." days");
}
could store it in an array with the day as the key and the +/-x days as the values.