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.
Related
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
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 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;
}
My server keeps returning the error because the function is not supported. I have recently found out that I cannot upgrade to PHP 5.3 until Zend Optimizer supports it; at the moment our server admins want to stick to the stable version of ZO for the server environment.
Can you recommend any adjustments that I can make to the code?
Fatal error: Call to undefined method DateTime::createfromformat()
This is the code:
$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'));
}
// check database for necessary updates
$update = mysql_query("SELECT * FROM rent WHERE colour='#3C0'");
while($row_update = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between do nothing
} else {
// date is not between so update
$update_result = mysql_query("UPDATE rent SET colour='#F0F0F0' WHERE id=" . $row_update['id'] . " && colour='#3C0'");
mysql_close($update_result);
}
}
How can I resolve this?
You could replace that big if/elseif block with this:
$today = date("w");
$min_mod = ($today < 3) ? -4 - $today : 3 - $today;
$max_mod = ($today < 3) ? 2 - $today : 9 - $today;
$min = strtotime("today 00:00:00 -".$min_mod." days");
$max = strtotime("today 23:59:59 +".$max_mod." days");
then below in your SQL loop:
//$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
//$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$compare = strtotime($row_update['pDate']);
if ($min < $compare && $max > $compare) {
// date is between do nothing
} else {
// code snipped
}
Try to replace DateTime::createFromFormat('d/m/Y', $xxx); with date('d/m/Y', strtotime($xxx));.
PHP <= 5.3 is no longer supported, so do try to convice your admins for upgrade ( let them choose - stable Zend Optimizer or stable PHP Interpreter - which is more critical? )
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";
}