So what i am trying to do is for the function to return a string like this
January 19-20, 2018
the variables all i have are the initial date and the number of nights. Too bad there's no way to getDate or getYear like js. Thanks for the help
You can use this custom function:
function showDates($startDate, $nights) {
$d1=new DateTime($startDate);
$d2 = new DateTime($startDate);
$d2->add(new DateInterval("P10D"));
if ($d2->format('Y') == $d1->format('Y')) {
$year = $d1->format('Y');
} else {
$year = $d1->format('Y') . '-' . $d2->format('Y');
}
if ($d2->format('m') == $d1->format('m')) {
$month = $d1->format('F');
} else {
$month = $d1->format('F') . '-' . $d2->format('F');
}
if ($d2->format('d') == $d1->format('d')) {
$day = $d1->format('d');
} else {
$day = $d1->format('d') . '-' . $d2->format('d');
}
return $month . ' ' . $day . ' ' . $year;
}
echo showDates("2017-10-05 12:00:00", 3);
Related
I'm trying to write a function that returns a random number for every new student registered, but each time the form is submitted the function returns a null value, and the DB does not accept null values, it ought to return something like this: DFA/SSS/22/1246 here's the code:
function createRegNumber()
{
$schname = "DFA";
$month = date("m");
$year = date("Y");
$new_year = substr($year, 2, 2);
$base_year = 2019; // Set a base when the intakes started
$intake = intval($year) - $base_year; // This will increase for every year
$increase_with = $intake++;
if ($month == '3') {
$intake += $increase_with;
$reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);
} else if ($month == '9') {
$increase_with++;
$intake += $increase_with;
$reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);
}
return $reg_no;
}
Looks like the issue is that you return $reg_no but unless $month == '3' or $month == '9' it never gets set. Most likely you want:
if ($month == '3') {
$intake += $increase_with;
} else if ($month == '9') {
$increase_with++;
$intake += $increase_with;
}
$reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);
return $reg_no;
I don't know what are the errors of this code because it's suddenly not accurate the showing value to the user like this:
$holidays = array();
$query_holiday = "SELECT * FROM holiday_tbl";
$result = mysqli_query($dbcon, $query_holiday);
while($row_holiday = mysqli_fetch_assoc($result))
{
array_push($row_holiday, $holidays);
}
if(strtotime(date("Y-m-d")) > strtotime($row['due_date']))
{
$currentDate = date("Y-m-d");
$days = (strtotime($currentDate) - strtotime($row['due_date'])) / 86400;
$daysTemp = $days;
for($i=1;$i<$days;$i++)
{
$currentDay = date("D", strtotime("+ ".$i." days"));
$date_loop = date("Y-m-d", strtotime("+ ".$i." days"));
if($currentDay == "Sun" || $currentDay == "Sat")
{
$daysTemp--;
}
else if(in_array($date_loop, $holidays))
{
$daysTemp--;
}
}
echo $daysTemp;
}
The current implementation is rather convoluted. In these cases I find it's always better to start over and try to simplify the logic as much as possible. Here is my take on your problem:
function calculate_days_late($due_date = '', $holidays = array(), $current_date = 'today') {
$days_late = 0;
// Get the timestamps for due date and current date
$current_date_ts = strtotime($current_date);
$due_date_ts = strtotime($due_date);
// If current date is not after due date then not late
if ($current_date_ts <= $due_date_ts) {
return $days_late;
}
$loop_date_ts = $current_date_ts;
while ($loop_date_ts > $due_date_ts) {
// If the looping date is not a weekend or holiday then count it
if ( ! in_array(date('D', $loop_date_ts), array('Sat','Sun'))
&& ! in_array(date('Y-m-d', $loop_date_ts), $holidays)) {
$days_late++;
}
$loop_date_ts = strtotime('-1 day', $loop_date_ts);
}
return $days_late;
}
// Test
echo '2017-09-05 = ' . calculate_days_late('2017-09-05', array(), '2017-09-05') . "\n";
echo '2017-09-06 = ' . calculate_days_late('2017-09-05', array(), '2017-09-06') . "\n";
echo '2017-09-07 = ' . calculate_days_late('2017-09-05', array(), '2017-09-07') . "\n";
echo '2017-09-08 = ' . calculate_days_late('2017-09-05', array(), '2017-09-08') . "\n";
echo '2017-09-09 = ' . calculate_days_late('2017-09-05', array(), '2017-09-09') . "\n";
echo '2017-09-10 = ' . calculate_days_late('2017-09-05', array(), '2017-09-10') . "\n";
echo '2017-09-11 = ' . calculate_days_late('2017-09-05', array(), '2017-09-11') . "\n";
echo '2017-09-12 = ' . calculate_days_late('2017-09-05', array(), '2017-09-12') . "\n";
echo '2017-09-13 = ' . calculate_days_late('2017-09-05', array(), '2017-09-13') . "\n";
Working example: https://eval.in/856018
Here is my code,
if ($_SESSION["crossfit"]=="Y") { if ($_SESSION["tr_duration1"]==1) { $dr1="1 Month"; } else {
if ($_SESSION["tr_duration1"]==12) { $dr1="1 Year"; } else
{ $dr1=$_SESSION["tr_duration1"] . ' Months'; }} $c1='Crossfit - ' . $dr1;}
if ($_SESSION["muaythai"]=="Y") { if ($_SESSION["tr_duration2"]==1) { $dr2="1 Month"; } else {
if ($_SESSION["tr_duration2"]==12) { $dr2="1 Year"; } else
{ $dr2=$_SESSION["tr_duration2"] . ' Months'; }} $c2=', Muaythai - ' . $dr2;}
if ($_SESSION["mma"]=="Y") { if ($_SESSION["tr_duration3"]==1) { $dr3="1 Month"; } else {
if ($_SESSION["tr_duration1"]==12) { $dr3="1 Year"; } else
{ $dr3=$_SESSION["tr_duration3"] . ' Months'; }} $c3=', MMA - ' . $dr3;}
if ($_SESSION["gymnastics"]=="Y") { if ($_SESSION["tr_duration4"]==1) { $dr4="1 Month"; } else {
if ($_SESSION["tr_duration4"]==12) { $dr4="1 Year"; } else
{ $dr4=$_SESSION["tr_duration4"] . ' Months'; }} $c4=', Gymnastics - ' . $dr4;}
$for = $c1 . $c2 . $c3 . $c4 . '.';
At least one of the programs will be 'Y' or all of them.
What I want is that $c1 $c2 $c3 $c4 come out separated by ',' and ends with '.' so it becomes readable.
My problem is if I put
$for = $c1 . $c2 . $c3 . $c4 . '.'; and the ',' with $c1 and select programs 2 and 3
I get
$for = , Muaythai - 3 Months, MMA - 12 Months.
of course I want,
$for = Muaythai - 3 Months, MMA - 12 Months.
If I put ',' in the $for directly and remove from $c1 if 2nd and 3rd programs are selected
$for = $c1 . ',' . $c2 . ',' . $c3 . ',' . $c4 . '.'
I get
$for = ,Muaythai - 3 Months, MMA - 12 Months,.
I want
$for = Muaythai - 3 Months, MMA - 12 Months.
I have also tried putting nesting so many isset's but haven't succeeded.
I have also tried by making a 'for' loop but ended up with more syntax errors than output errors
It works fine only if I select only the first program or all the 4 programs.
Try something like this:
$tmp = array($c1,$c2,$c3,$c4);
$filtered = array_filter($tmp);
$for = implode(",", $filtered).".";
This will strip out any $c_ variables that aren't populated, and separate them with commas.
EDIT: I have reivewed your code, and would like to suggest this optimised version:
$keys = array("Crossfit","Muaythai","MMA","Gymnastics");
$result = array();
foreach($keys as $i=>$name) {
if( $_SESSION[strtolower($name)] == "Y") {
$duration = $_SESSION['tr_duration'.($i+1)];
switch($duration) {
case 1: $dur = "1 Month"; break;
case 12: $dur = "1 Year"; break;
default: $dur = $duration." Months";
}
$result[] = $name." - ".$dur;
}
}
if( !$result) $for = "Nothing selected!";
else $for = implode(", ",$result).".";
If you need help understand what's going on here, feel free to ask!
you can do it using single variable and concate values to it.
try like this:
for = "";
if ($_SESSION["crossfit"] == "Y") {
if ($_SESSION["tr_duration1"] == 1) {
$dr1 = "1 Month";
} else {
if ($_SESSION["tr_duration1"] == 12) {
$dr1 = "1 Year";
} else {
$dr1 = $_SESSION["tr_duration1"] . ' Months';
}
}
$for .= 'Crossfit - ' . $dr1;
}
if ($_SESSION["muaythai"] == "Y") {
if ($_SESSION["tr_duration2"] == 1) {
$dr2 = "1 Month";
} else {
if ($_SESSION["tr_duration2"] == 12) {
$dr2 = "1 Year";
} else {
$dr2 = $_SESSION["tr_duration2"] . ' Months';
}
}
$for .= empty($for) ? 'Muaythai - ' . $dr2 : ', Muaythai - ' . $dr2;
}
if ($_SESSION["mma"] == "Y") {
if ($_SESSION["tr_duration3"] == 1) {
$dr3 = "1 Month";
} else {
if ($_SESSION["tr_duration1"] == 12) {
$dr3 = "1 Year";
} else {
$dr3 = $_SESSION["tr_duration3"] . ' Months';
}
}
$for .= empty($for) ? 'MMA - ' . $dr3 : ', MMA - ' . $dr3;
}
if ($_SESSION["gymnastics"] == "Y") {
if ($_SESSION["tr_duration4"] == 1) {
$dr4 = "1 Month";
} else {
if ($_SESSION["tr_duration4"] == 12) {
$dr4 = "1 Year";
} else {
$dr4 = $_SESSION["tr_duration4"] . ' Months';
}
}
$for .= empty($for) ? 'Gymnastics - ' . $dr4 : ', Gymnastics - ' . $dr4;
$c4 = ', Gymnastics - ' . $dr4;
}
you should use switch case instead of many if else.
The code works perfect, but since I'm repeating myself (which I like to avoid while programming), I was wondering if this could be written any shorter/better?
$start = getLocaleDate($item[0]['start_day']);
$start = $start['day_int'] . ' ' . $start['month_string'];
if ($item[0]['start_houre'] !== '00:00:00') {
$houre = stripLeadingZero(substr($item[0]['start_houre'], 0, 2));
$minute = substr($item[0]['start_houre'], 3, 2);
$start .= ' at' . $houre . 'u' . $minute;
}
$end = getLocaleDate($item[0]['end_day']);
$end = $end['day_int'] . ' ' . $end['month_string'];
if ($item[0]['end_houre'] !== '00:00:00') {
$houre = stripLeadingZero(substr($item[0]['end_houre'], 0, 2));
$minute = substr($item[0]['end_houre'], 3, 2);
$end .= ' at' . $houre . 'u' . $minute;
}
Without changing any of the functionality, you could make it into a function:
function getTime($item, $which) {
$time = getLocaleDate($item[0][$which . '_day']);
$time = $time['day_int'] . ' ' . $time['month_string'];
if ($item[0][$which . '_houre'] !== '00:00:00') {
$houre = stripLeadingZero(substr($item[0][$which . '_houre'], 0, 2));
$minute = substr($item[0][$which . '_houre'], 3, 2);
$time .= ' at' . $houre . 'u' . $minute;
}
return $time;
}
$start = getTime($item, 'start');
$end = getTime($item, 'end');
* It should be noted that this code doesn't do any error checking/prevention though, so if there is no index 0 in the $item, you will have an error (same goes for $item[0]['start_day'], $item[0]['end_day'], etc). To handle a simple-case, you could add if (!isset($item[0])) return ''; to the beginning of the function, if it's a concern.
sure could you can write a function where you pass in your item and the key to use
function your_function($item, $key) {
$h = $item[$key.'_houre'];
$time = getLocaleDate($item[$key. '_day']);
$time = $end['day_int'] . ' ' . $end['month_string'];
if ($h !== '00:00:00') {
$houre = stripLeadingZero(substr($h , 0, 2));
$minute = substr($h , 3, 2);
$time .= ' at' . $houre . 'u' . $minute;
return $time;
}
}
your_function($item[0], 'end');
your_function($item[0], 'start');
Few days ago server crashed and was down for few hours, after server become available, my calendar started to display wrong data. It had to show me current month and 5 next(half a year in total). Server data is correct. Any ideas whats wrong with calendar? Does mysql server time can make my calendar show wrong data?
if (!isset($_MONTH))
$_MONTH = 6;
if (isset($_POST['subscribe_month']))
$_MONTH = $class->_dig($_POST['subscribe_month']);
$sql = mysql_query("SELECT d.header, d.id FROM " . $class->_cfg['pfx'] . "workshops as w
LEFT JOIN " . $class->_cfg['pfx'] . "workshops_date as wd ON wd.cid=w.id
LEFT JOIN " . $class->_cfg['pfx'] . "dictionary as d ON d.id=wd.city
WHERE w.public='1' and wd.public='1' and wd.date_end>='" . date("Y-m-d") . "' a
nd wd.predprosomtr='0' " . $where . " ORDER BY d.rang");
$CityList = array();
while ($_sql = mysql_fetch_assoc($sql)) {
$CityList[$_sql['id']] = $_sql['header'];
}
if ($Fcity && $Fcity != 0)
$where.=" and d.id=" . $Fcity . "";
elseif ($_POST['city'] && $class->_dig($_POST['city']) > 0)
$where.=" and d.id=" . $class->_dig($_POST['city']) . "";
if ($CitySearch != 0)
$where.=" and wd.city=" . $CitySearch . " ";
$sql = mysql_query("SELECT w.header, w.direction, w.subheader, wd.colsmonth, wd.is_new, wd.public_date_finish, wd.p_date_finish, w.aliaslink, wd.aliaslink as wd_aliaslink, w.direction, wd.city, wd.date_start, d.header as city_name, wd.date_end, wd.cid, wd.id as wd_id, w.id as w_id FROM " . $class->_cfg['pfx'] . "workshops as w
LEFT JOIN " . $class->_cfg['pfx'] . "workshops_date as wd ON wd.cid=w.id
LEFT JOIN " . $class->_cfg['pfx'] . "dictionary as d ON d.id=wd.city
WHERE w.public='1' and wd.public='1' and wd.date_end>='" . date("Y-m-d") . "' and w.direction<>'' and wd.predprosomtr='0' " . $where . " ORDER BY wd.date_start, wd.city");
//$output.=$where;
$month = 12;
$year = date("Y");
while ($_sql = mysql_fetch_assoc($sql)) {
$view = true;
if ($_sql['public_date_finish'] == '1' && $_sql['p_date_finish'] < date("Y-m-d"))
$view = false;
if ($view) {
$arWorkshops[$_sql['w_id']] = $_sql;
if (!isset($arWorkshopsCity[$_sql['cid']][$_sql['city']]) && $_sql['city'] > 0)
$arWorkshopsCity[$_sql['cid']][$_sql['city']] = $_sql['city'];
if (isset($arWorkshopsDate[$_sql['cid']]['count']))
$arWorkshopsDate[$_sql['cid']]['count'] = $arWorkshopsDate[$_sql['cid']]['count'] + 1;
else
$arWorkshopsDate[$_sql['cid']]['count'] = 1;
$direct = explode('#', $_sql['direction']);
for ($i = 0; $i < count($direct); $i++) {
if (trim($direct[$i]) != '') {
$arDirectionList[$direct[$i]] = $direct[$i];
}
}
//$arDirectionList[$_sql['direction']]=$_sql['direction'];
if (!isset($arWorkshopsDate[$_sql['cid']][ceil($class->convert_date($_sql['date_start'], '.', "month"))]))
$arWorkshopsDate[$_sql['cid']][ceil($class->convert_date($_sql['date_start'], '.', "month"))] = $_sql;
if ($class->convert_date($_sql['date_start'], '.', "month") < $month && $class->convert_date($_sql['date_start'], '.', "year") == $year) {
$month = $class->convert_date($_sql['date_start'], '.', "month");
$year = $class->convert_date($_sql['date_start'], '.', "year");
}
//if($class->convert_date($_sql['date_start'], '.', "year")==(date("Y")+1))$year=$class->convert_date($_sql['date_start'], '.', "year");
//$output.=$_sql['header']."-".$_sql['date_start']."-".$_sql['wd_id']."<br>";
}
}
//var_dump($arWorkshopsDate[185]);
$output.='<table class="table"><tr><th width="60"> </th><th class="first" style="width:auto">Название</th>';
if ($_MONTH == 12)
$w = "5%"; else
$w = "9%";
for ($i = $month; $i < ($month + $_MONTH); $i++) {
if ($year == date("Y"))
$m = $i;else
$m++;
if ($m <= 12 && $year == date("Y")) {
$output.='<th class="month" style="width:' . $w . '">' . $class->convert_date($m, '.', "myear") . ' <span>' . $year . '</span></th>';
} else {
if ($m > 12) {
$m = 1;
$year = $year + 1;
}
$output.='<th class="month" style="width:' . $w . '">' . $class->convert_date($m, '.', "myear") . ' <span>' . $year . '</span></th>';
}
}
$output.=' <th style="width:10%">';
if ($typeblock == 'webinars')
$output.='Формат';
else
$output.='Город';
$output.='</th></tr>';
if (isset($arWorkshops)) {
//foreach($arWorkshops as $LO=>$listOrd){
foreach ($arWorkshops as $k => $value) {
if (!$direction || $direction == 0)
$direction2 = $class->_direction($value['direction']);
else
$direction2 = $direction;
foreach ($arWorkshopsCity[$k] as $LO => $listOrd) {
$output2 = "";
$link_date = "";
$pt = 0;
$m_nth = ceil($month);
$is_new_class = '';
for ($i = ceil($month); $i < ($month + $_MONTH); $i++) {
if ($m_nth > 12)
$m_nth = 1;
if (isset($arWorkshopsDate[$k][$m_nth]) && $arWorkshopsDate[$k][$m_nth]['city'] == $LO) {
$pt++;
if ($pt == 1)
$city_name = $arWorkshopsDate[$k][$m_nth]['city_name'];
//if(isset($_TYPE) && $_TYPE!=0)$output.='<td class="date"><a href="/'.$typeblock.'/'.$value['aliaslink'].'/'.$arWorkshopsDate[$k][$i]['wd_aliaslink'].'/">';
//else
if (($arWorkshopsDate[$k][$m_nth]['is_new'] == '1') || ($arWorkshopsDate[$k][$m_nth + 1]['is_new'] == '1')) {
$is_new_class = " it_is_new";
} else {
$is_new_class = '';
}
$output2.='<td class="date"><a href="/' . $typeblock . '/' . $arDictionaryID[$direction2]['aliaslink'] . '/' . $value['aliaslink'] . '/' . $arWorkshopsDate[$k][$m_nth]['wd_aliaslink'] . '/">';
$link_date = '/' . $typeblock . '/' . $arDictionaryID[$direction2]['aliaslink'] . '/' . $value['aliaslink'] . '/' . $arWorkshopsDate[$k][$m_nth]['wd_aliaslink'] . '/';
if ($arWorkshopsDate[$k][$m_nth]['colsmonth'] > 0)
$output2.=$class->convert_date($arWorkshopsDate[$k][$m_nth]['date_start'], '.', "day_month") . "</a><br />" . $arWorkshopsDate[$k][$m_nth]['colsmonth'] . " мес.";
elseif ($arWorkshopsDate[$k][$m_nth]['date_start'] == $arWorkshopsDate[$k][$m_nth]['date_end'])
$output2.=$class->convert_date($arWorkshopsDate[$k][$m_nth]['date_start'], '.', "day_month") . "</a>";
else
$output2.=$class->convert_date($arWorkshopsDate[$k][$m_nth]['date_start'], '.', "day_month") . "<br />-" . $class->convert_date($arWorkshopsDate[$k][$m_nth]['date_end'], '.', "day_month") . "</a>";
$output2.='</td>';
}else {
$output2.='<td></td>';
}
$m_nth++;
}
if (($arWorkshopsDate[$k][$m_nth]['is_new'] == '1')) {
$is_new_class = " it_is_new";
}
$output.='<tr><td class="' . $is_new_class . '"> </td><td>';
//if(isset($_TYPE) && $_TYPE!=0)$output.='<strong>'.$value['header'].'</strong>';
//else
if ($pt == 1 && $arWorkshopsDate[$k]['count'] == 1)
$link = $link_date;
else
$link = '/' . $typeblock . '/' . $arDictionaryID[$direction2]['aliaslink'] . '/' . $value['aliaslink'] . '/';
$output.='<a href="' . $link . '"><strong>' . $value['header'] . '</strong>';
if (trim($value['subheader']) != '')
$output.=': ' . $value['subheader'] . '';
$output.='</a>';
$output.='</td>';
$output.=$output2;
$output.=' <td class="city">' . $city_name . '</td></tr>';
}
}
//}
}
$output.='</table>';
?>
Issue solved. By default some "good" coder made it show only from 12th month in $month = 12;
I just changed it to $month = date("m"); And this solved my issue