How to get the date values from database inside foreach loop - php

It's already working but the holidays only.I already connected the database inside foreach loop and minus the holiday in fines the table in my database and table in my website already count it as holiday table in website but after I add another holiday in database like September 6 and the result is like this "imgur.com/a/l8Deq" not counted as holiday and don't know why my fines and days are increasing. What should I do? Thank you for helping me
$borrowdate = new Datetime($row['date_return'],new DateTimeZone('Asia/Manila'));
$returndate = new Datetime($row['due_date'],new DateTimeZone('Asia/Manila'));
$currentdate = new Datetime('Asia/Manila');
$returndate->setTime(0,0);
$currentdate->setTime(0,0);
$borrowdate->setTime(0,0);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($returndate, $interval, $borrowdate);
$borrowdate->format("D");
$weekendDays = 0;
$totalDays = 0;
$holiDays = 0;
$query_holiday =mysqli_query($dbcon,"SELECT * FROM holiday_tbl");
while($row=mysqli_fetch_array($query_holiday)){
$holi = $row ['holiday'];
foreach ($period as $p )
{
$totalDays++;
if($p->format( "w" )== 0 or $p->format( "w" )==6 ) $weekendDays++;
if($p->format('Y-m-d') == $holi) $holiDays++;
}
}
echo "<p>Total days: $totalDays</p><p>Weekend days: $weekendDays </p> <p> Holidays: $holiDays </p>";
$fines = ($totalDays - $weekendDays - $holiDays) * 5;
echo "₱ ". $fines;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");

Related

PHP: find dates every week for different days

I am trying to solve a problem I am looking for hours into.
My students have special courses and I am trying to find the dates.
For example:
Student 1 in Class A: every Monday from Jan 1st till April 1st
Student 2 in Class B: every Wednesday from April 1st until June...
So I programmed a function in which I can pass info like begin, end, weekday to show me the dates:
function tkcheck ($beginnfunc,$endfunc,$daycheck)
{
$begin = new DateTime($beginnfunc);
$end = new DateTime($endfunc);
$interval = new DateInterval('P1W');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $date) {
$dayw = $date->modify($daycheck);
if ($dayw < $end) {
$daystring = $dayw->format ('d-m-Y');
$q1day1[] = $daystring;
}
}
}
tkcheck ('2022-02-20','2022-04-01','next Wednesday');
print_r($q1day1);
But print_r does not show me any information when I try to use my function tkcheck...
Maybe some here might help me, thank you!
$q1day1 is scoped within the function. It is not accessible out of the function.
To fix this return the variable.
function tkcheck ($beginnfunc,$endfunc,$daycheck)
{
$begin = new DateTime($beginnfunc);
$end = new DateTime($endfunc);
$interval = new DateInterval('P1W');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $date) {
$dayw = $date->modify($daycheck);
if ($dayw < $end) {
$daystring = $dayw->format ('d-m-Y');
$q1day1[] = $daystring;
}
}
return $q1day1;
}
$result = tkcheck ('2022-02-20','2022-04-01','next wednesday');
print_r($result);
See Variable Scope for more info.
The special feature of this solution is simply to create a Date::Interval from 'next Monday'.
$start = date_create('Jan 1st 2022')->modify('-1 Day');
$end = date_create('Apr 1st 2022');
$interval = DateInterval::createFromDateString('next Monday');
$period = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE);
$dateArray = iterator_to_array ($period);
echo '<pre>'.var_export($dateArray,true).'</pre>';
The modification with "-1 Day" is necessary to record a Monday that falls exactly on the start date.

How to section a date period monthly

two dates 13-10-2017 and 13-02-2018. I want to separate this period in months like 13-10-2017 to 31-10-2-17, 01-11-2017 to 30-11-2017, 01-12-2017 to 31-12-2017, 01-01-2018 to 31-01-2018 and 01-02-2018 to 13-02-2018. What I did I can get the month names in the date period but not in the format I want.
Here is my code:
$start_date = new DateTime('13-10-2017');
$end_date = new DateTime('13-02-2018');
$date_interval = new DateInterval('P1M');
$date_period = new DatePeriod($start_date, $date_interval, $end_date);
# calculating number of days in the interval
$interval = $start_date->diff( $end_date );
$days = $interval->days;
# getting names of the months in the interval
$month_count = 0;
$month_names = array();
foreach ($date_period as $date) {
$month_names[] = $date->format('F');
$month_count++;
}
$month_name_string = implode(',', $month_names);
echo $start_date->format('d-m-Y').' to '.$end_date->format('d-m-Y'). ' is ' .$days.' days and month names are: '.$month_name_string;
The output I get :
13-10-2017 to 13-02-2018 is 123 days and month names are: October,November,December,January
You can, while iterating, do the following checks:
If the current month is in $start_date, use its day for the start date
If the current month is in $end_date, use its day for the last day
Else, use the 1 and maximum day of each month (using the t format character)
Also, you need to set the time to 00:00:01 in the final day in order to have it considered in the DateInterval:
<?php
$start_date = new DateTime('13-10-2017');
$end_date = new DateTime('13-02-2018');
$end_date->setTime(0, 0, 1); // important, to consider the last day!
$date_interval = new DateInterval('P1M');
$date_period = new DatePeriod($start_date, $date_interval, $end_date);
# calculating number of days in the interval
$interval = $start_date->diff( $end_date );
$days = $interval->days;
# getting names of the months in the interval
$dates = [];
foreach ($date_period as $date) {
$dateArr = [];
if ($date->format("Y-m") === $start_date->format("Y-m")) {
$dateArr["start"] = $start_date->format("d-m-Y");
}
else {
$dateArr["start"] = $date->format("01-m-Y");
}
if ($date->format("Y-m") === $end_date->format("Y-m")) {
$dateArr["end"] = $end_date->format("d-m-Y");
}
else {
$dateArr["end"] = $date->format("t-m-Y"); // last day of the month
}
$dates[] = $dateArr;
}
foreach ($dates as $date) {
echo $date["start"]." to ".$date["end"].PHP_EOL;
}
Demo
You can employ DateTime::modify function. E.g.:
$month_intervals = [];
foreach ($date_period as $date) {
$start = $date == $start_date ? $start_date : $date->modify('first day of this month');
$month_intervals[] = join([
$start->format('d-m-Y'),
$date->modify('last day of this month')->format('d-m-Y')
], ' to ');
}
$month_intervals[] = join([
(clone $end_date)->modify('first day of this month')->format('d-m-Y'),
$end_date->format('d-m-Y')
], ' to ');
echo implode(',', $month_intervals);

How to not calculate the weekend in penalty using php [duplicate]

This question already has answers here:
How to put a borrowdate into my calculation fines
(1 answer)
PHP day of week numeric to day of week text
(8 answers)
Closed 5 years ago.
I want to prevent calculation in fines when the day is Saturday and Sunday or Only Sunday not include in fines. I think it's better if we use the borrowdate,currentdate, and returndate to fully functions from my database Thank you!
$borrowdate = new Datetime($row['date_return']);
$returndate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$fines = 0;
if($currentdate > $returndate){
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;
echo "₱ ". $fines = $days > 0 ? intval(floor($days)) * 15 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
You should count the number of working days, and than calculate your fine.
$returnDate= new DateTime( '2017-08-17' );
$today= new DateTime( '2017-08-21 00:23:44' );
$returnDate->setTime(0,0);
$today->setTime(0,0);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($returnDate, $interval, $today);
$weekendDays = 0;
$totalDays = 0;
foreach ( $period as $p ) {
$totalDays++;
if($p->format( "w" )==0 or $p->format( "w" )==6) $weekendDays++; //6 - saturday, 0 - sunday
}
echo "<p>Total days: $totalDays</p><p>Weekend days: $weekendDays </p>";
$fines = ($totalDays - $weekendDays) * 15;
echo "₱ ". $fines;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
do like this first find out the dayofweek of current date and check if its sunday or saturday. The value returned form day of week will be in number. 0 means Sunday and through 6 for saturday.
$borrowdate = new Datetime($row['date_return']);
$returndate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$dayofweek = date('w', $currentdate->getTimestamp());
$fines = 0;
if($currentdate > $returndate){
if($dayofweek != 0){
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;
echo "₱ ". $fines = $days > 0 ? intval(floor($days)) * 15 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
}
$newquery = mysqli_query($dbcon,"select fines from borrowdetails where borrow_details_id = '$fi'");
$row = mysqli_fetch_assoc($newquery);
echo $row['fines'];

Calculating date difference when working with a set of business hours in PHP

I'm working on a help desk app. Users create tickets, and I'm trying to figure out how to code the Due By time.
Say I have a business hours array that looks like this:
$bh['Monday']['start_time'] = "8:00am"
$bh['Monday']['end_time'] = "5:00pm"
$bh['Tuesday']['start_time'] = "8:00am"
$bh['Tuesday']['end_time'] = "5:00pm"
$bh['Wednesday']['start_time'] = "8:00am"
$bh['Wednesday']['end_time'] = "5:00pm"
$bh['Thursday']['start_time'] = "8:00am"
$bh['Thursday']['end_time'] = "5:00pm"
$bh['Friday']['start_time'] = "8:00am"
$bh['Friday']['end_time'] = "5:00pm"
And an array with holidays:
$holidays = array("Dec 25th","Nov 24th")
I also have two other variables:
$resolve_within = "5";
$resolve_within_duration = "days";
What I have so far:
$resolve_within = "5";
$resolve_within_duration = "days";
$holidays = array("Dec 25th","Nov 24th")
$bh['Monday']['start_time'] = "8:00am"
$bh['Monday']['end_time'] = "5:00pm"
$bh['Tuesday']['start_time'] = "8:00am"
$bh['Tuesday']['end_time'] = "5:00pm"
$bh['Wednesday']['start_time'] = "8:00am"
$bh['Wednesday']['end_time'] = "5:00pm"
$bh['Thursday']['start_time'] = "8:00am"
$bh['Thursday']['end_time'] = "5:00pm"
$bh['Friday']['start_time'] = "5:00pm"
$bh['Friday']['end_time'] = "5:00pm"
$start = new DateTime('now'); //when the ticket is created
$end = new DateTime('now');
$end->modify('+'.$resolve_within.' '.$resolve_within_duration); //this is essentially the due by time, adding 5 days to 'now'
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
foreach($period as $dt) {
$curr = $dt->format('D');
//adjust days for holidays
if (in_array($dt->format('M jS'), $holidays)) {
$days--;
}
}
echo "Ticket is due in $days days.";
So, given the start date and end date, how can I come up with the due by time, excluding dates in the $holidays array, and also taking for account the times and days in the $bh (business hours) array?
For example, if someone creats a ticket on Wed 8/23/16 at 3:00pm, and there's a Holiday on Friday. The due by time should be Thursday 8/11/16 at 3:pm. Keeping in mind also that the duration can be hours and not days.
For example, some tickets by be tagged as Urgent, therefore the $resolve_within value might be "4" and the $resolve_within_duration will be "hours"

sql search whilst in a foreach loop or while loop

Hi firstly heres my code.
<?php
function getDatesBetween2Dates($startTime, $endTime) {
$day = 86400;
$format = 'd-m-Y';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1;
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
///
$days = getDatesBetween2Dates(date('d-m-Y', strtotime('-3 weeks Monday')),date('d-m-Y', strtotime('+2 weeks Sunday')));
foreach($days as $key => $value){
$dayNumber = date('d', strtotime($value));
//echo $value;
echo "<div id=\"day\">
<div id=\"number\">$dayNumber</div>";
////////////sql seearch//\\\/////////
//Connect to db
include("../djwbt.php");
$sql = "SELECT * FROM daysummary WHERE date='$value'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$place = $row['place'];
$invoicedate = $row['date'];
}
/////////////end sql search//////////
echo "<div id=\"event\">$place</div>
</div><!-- end day -->";
}
?>
What i am trying to do is show all dates between two points and for each of the dates search my db using the date as a where clause. i have tried putting the search in a few places but im not getting the right results.
this gives me the same result in each date.
e.g. 17th = (empty) as in my db, 18TH = HOME (as in my db), 19th = HOME (not as in my db), 20th = HOME (this continues all the way through fore each)
the link in each fore each works perfectly?
Any help would be amazing.
I would make one statement that gets all the needed data from your database:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
then use the foreach loop for the results
Note that mysql_ functions are deprecated, Try switching to mysqli_ or PDO

Categories