Row highlighting based on due date using php - php

I'm relatively new to PHP, I've learned procedural PHP and I'm playing around with an app.
I've Looked around the site and found a few samples on date formatting and highlighting using PHP, HTML and CSS.
With the help found here and the PHP manual, I've put together some code to highlight 2 different rows among many others provided by a database that follow this criteria:
Anything due in 1 day (today, yesterday, last week, etc.) should color the table row red.
Anything 3 days out (between 1 day and 3 days in the future) should color the table row yellow.
Anything else should utilize the bootstrap "table-striped" styling.
Here is the code I've put together
//Today + 1 day
$oneDay = date('m/d/Y', strtotime("+1 day"));
//Today + 3 days
$threeDays = date('m/d/Y', strtotime("+3 days"));
//Database entry for comparison
$due = date_create($record['projected_delivery']);
$dueOneDay = date_create($oneDay);
$dueThreeDays = date_create($threeDays);
//Get the difference between the 2 dates
$diffOne = date_diff($due, $dueOneDay);
$diffThree = date_diff($due, $dueThreeDays);
if ($diffThree->format('%d') < 3) {
$highlight_css = " warning";
}elseif ($diffOne->format('%d') <= 1){
$highlight_css = " danger";
}else {
$highlight_css = "";
}
I then add the $highlight_css to the HTML.
So far some of the functionality is working. Proper highlighting is not added for:
Dates older than 1 day (i.e. yesterday, last week)
How can this functionality be achieved?

I would use this instead:
<?php
$dueDates =["06/05/2017","06/06/2017","06/07/2017","06/08/2017","06/09/2017","06/10/2017","06/11/2017","06/12/2017"];
$table = "<table class='table table-bordered'><thead><tr><th>Date</th></tr></thead><tbody>";
$today = date_create(date('m/d/Y')); // example: 06/07/2017
foreach ($dueDates as $dueStr) {
$due = date_create($dueStr);
$diff = date_diff($today, $due)->format("%r%a");
$highlight_css = "";
if ($diff > 1 && $diff <= 3) {
$highlight_css = " warning";
} elseif ($diff == 1) {
$highlight_css = " danger";
}
$table .= "<tr class='$highlight_css'><td>$dueStr</td></tr>";
}
$table .= "</tbody></table>";
?>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" >
<?php
echo $table;

$diffThree->format('%d')
will return a string instead of an integer. In order to compare it correctly with other numbers (3 and 1 in your case) you need to convert it to an int:
(int) $diffThree->format('%d')
and
(int) $diffOne->format('%d')

The issue
A DateInterval (the return value of date_diff()) has the number of days (actually stored in a property d, which could be used instead of getting a string with format()), which should always be a positive number. And for dates before today's date, the first date diff (i.e. $diffOne) will be 2+ days and the second date diff (i.e. $diffThreeDays) will be 4+ (see this playground example with debugging output above the table) so days before today's date will never be associated with either 'warning' or 'danger' according to the given logic.
A Solution
One approach (perhaps simpler) to comparing the dates is to use DateTime comparison
Note:
As of PHP 5.2.2, DateTime objects can be compared using comparison operators.1
So compare the DateTime variables (i.e. $due with $dueOneDay and $dueThreeDays) instead of the date differences.
In the logic below, you will notice that the order of the comparisons has been reversed, so the check for one day difference comes before the check for three days difference, in order to avoid the danger case never happening.
if ($due < $dueOneDay){
$highlight_css = " danger";
}else if ($due <= $dueThreeDays) {
$highlight_css = " warning";
}else {
$highlight_css = "";
}
With this approach, there is no need for the date_diff variables (i.e. $diffOne, $diffThree).
See a demonstration of this in this playground example.
1http://php.net/manual/en/datetime.diff.php#example-2524

Related

Finding the difference between 2 dates in PHP

I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.
BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the table with the data.
For this I have this check:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
die();
}
The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:
11/14/2015 (start date) && 12/14/2015 (end date) - should show data
09/14/2015 (start date) && 12/14/2015 (end date) - should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) - should not show data but does
There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future! <br />";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
Edit
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not
In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.
The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:
$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';
Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.

Conditional DateDiff

I had visited some of the post retrieving the date difference between 2 dates in SO but it doesn't gave me the answer I was seeking. Same for reading the documentation, I had problem understanding how it works.
I have tried coding it but it doesn't behave like what I was expecting. Here is my code:
<?php
$currentDate = new DateTime();
$createDateJoin = date_create($getDate['date_joined']);
$dateJoin = date_format($createDateJoin, "Y-m-d");
$yearDifference = $currentDate->diff($createDateJoin);
if ($yearDifference->d < 31 && $yearDifference->m = 0 && $yearDifference->y == 0) {
echo $yearDifference->d . " days";
} else if ($yearDifference->m > 3) {
echo $yearDifference->m . " month";
} else if ($yearDifference->y > 1) {
echo $yearDifference->y . " years";
} else {
echo "Not yet assigned";
}
?>
As you can see from my code above, I am trying to do a print when after calculating the difference between the 2 dates, it meets the condition of $yearDifference->.The behavior from the program that I have experienced does not print out the things I want accordingly (E.g Staff working more than 1 year will print out how many years they have work, months for those who just came in and new staff less than a month will print out days).
I would like to know how does ->d/m/y works and how can I actually make use of the d,m and y to draw out the specific date correctly. And I also noticed that when I treat $yearDifference as a String or int, it comes out different result for the conditions. So what should I treat the type to be to manipulate it more easily? Greatly appreciate the help.
You can use this code to get the date different method diff() object returns more values to check you can print_r your object that will print all data member that are returned via diff() method
<?php
echo get_date_diff(strtotime('1990-10-12'),strtotime('2015-10-14'));
function get_date_diff($date,$dateEnd) {
$dStart = new DateTime(date("Y-m-d", $date));
$dEnd = new DateTime(date("Y-m-d", $dateEnd));
$dDiff = $dStart->diff($dEnd);
return($dDiff->y.' years <br>'.$dDiff->m.' months <br>'.$dDiff->d. ' days');
}
?>

how can i get all next dates with day from given date which is based on weekly, biweekly, monthly in php?

I want all next dates with day from specified date for sending mail within cron job file.
right now I m checking for whether its week by calculating like this
$event_from_date = strtotime(date('Y-m-d',strtotime($rs->from_date)));
$today_date = strtotime(date('Y-m-d'));
$event_expire_on = strtotime($rs->to_date);
if($rs->time_zone == "CST")
$current_date=strtotime($cst_date);
elseif($rs->time_zone == "PST")
$current_date=strtotime($pst_date);
elseif($rs->time_zone == "MST")
$current_date=strtotime($est_date);
elseif($rs->time_zone == "EST")
$current_date=strtotime($mst_date);
if($current_date <= $event_expire_on && $current_date >= $event_from_date)
{
$diff=$today_date-$event_from_date;
if($diff%604800 == 0 && $rs->report_cycle=="Weekly")
$flag = 1;
else
$flag = 0;
}
can any body tell me . how can I get all next 7 days for weekly,15 days for biweekly,30 days for monthly with its day like Monday,Tuesday..
I'm not 100% sure I have understood your question. The DateTime classes offer simple ways of manipulating dates and times in all manner of ways.
I think, in your position I would be looking for DatePeriods to represent the various erm, periods I need. A function like this may suit:-
/**
* #param Int $days
* #return DatePeriod
*/
function getNumDays($days)
{
$today = new \DateTime('UTC');
$oneDay = new \DateInterval('P1D');
return new \DatePeriod($today, $oneDay, $days);
}
foreach(getNumDays(7) as $day){
//Do what ever you want with the DateTime instance
//We'll just var_dump() it for now.
var_dump($day);
}
See it working.

sql php confuse complex

PHP code (relevant)
$db = '2011-02-28'; $a = '2011-02-01';
and this is the part of the query
LEFT JOIN
abc
ON
abc.date between '$a' and '$db'
This query show following results :
1 2011-02-08
6 2011-02-09
6 2011-02-11
1 2011-02-13
but what i want is to get 0 as a result if there's n rows for other dates.
You can't do that (not without joining a table containing all dates, at least).
Displaying the 0s for dates with no data is the job of your application, not the database. It's trivial, just write a loop from the start date to the end date, and output the 0 for those dates with no rows in your result set.
SQL's job is to tell you what data is there, not what isn't.
while ($row = mysql_fetch_array($result)) {
$results[$row['date']] = $row['count'];
}
for ($time = strtotime('2011-02-01'); $time <= strtotime('2011-02-28'); $time += 86400) {
$datestr = date('Y-m-d', $time);
if (isset($results[$datestr])) {
echo "Count for date $datestr: " . $results[$datestr];
} else {
echo "Count for date $datestr: 0";
}
}
From the limited information you give about what problem you are trying to solve I would guess you are trying to find "free appointments" or similar.
If so then this should help you out. http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#98
This website has a number of query patterns that will help your thinking and design of your database.

Determine if a week is odd or even

I have debugged this legacy code, and would like a sanity check on it.
The purpose of it is to allow someone to choose a delivery frequency for shipping a product. If someone wants their product Every Other Week, the system needs to determine if they should get an order next week, or two weeks from now. We call it A week, or B Week.
Keep in mind I did not write this, I am just trying to make sense of it and would like some help evaluating its accuracy:
if (date("l") == "Monday" ) {
$start = 0;
} else if (date("l") == "Tuesday" || date("l") == "Wednesday" || date("l") == "Thursday" || date("l") == "Friday" || date("l") == "Saturday"|| date("l") == "Sunday") {
$start = -1;
}
// if changing to every other week set to next week's a/b-ness
$a_week_tid = 34;
$b_week_tid = 35;
$every_other_week_frequency_id = 32;
if ($delivery_frequency == $every_other_week_frequency_id) {
$julian = (int) (strtotime('Monday +' . $start . ' week') / 86400);
$julian_week = ($julian-4) / 7;
if ($julian_week % 2) {
$today_a_or_b = $b_week_tid;
$next_week_a_or_b = $a_week_tid;
$a_or_b_week_string = '(A Week)';
} else {
$today_a_or_b = $a_week_tid;
$next_week_a_or_b = $b_week_tid;
$a_or_b_week_string = '(B Week)';
}
} else {
$next_week_a_or_b = NULL;
$a_or_b_week_string = NULL;
}
This code is not commented or documented. The part that confuses me is:
Why is 4 subtracted from Julian, then divided by 7?
If today is Monday, $julian_week is 2129, and 2129 % 2 evaluates TRUE. Is that correct?
Is this even how it should be done? Can't I rewrite this using date('w') a lot easier?
Yeah using date would totally be easier, plus it takes into account leap years, daylight saving time, all that extra stuff you don't want to have to deal with.
if (date('W')%2==1)
That's SOOOO much easier to maintain than the above.
I don't believe you can use date("W") in this case. According to the ISO calculation, on occasion, there will be years with 53 weeks. In those years, Week 53 is followed by Week 01, both odd numbers, and an A/B calculation based on Even/Odd ISO week number would result in two successive A or B weeks.
The original calculation determines the number of days from the UNIX epoch of the present Monday, or of the most recent Monday if today is not a Monday. The -4 causes the A/B week labels to change on Thursdays. Even/oddness of a week is determined from a fixed date (the Unix Epoch), so there will be no discontinuity in the oscillation of A/B-ness using the original code.
The ISO standard for week one in a year is that it is the week that the first Thursday of the year falls. This is the reason for the 4 subtracted from the Julian date. The week number is then found by dividing by 7.
Again the ISO standard implies that week number cannot be greater than 53. I don't understand how your figure of 2129 can arise. However the div operator will not evaluate TRUE for this figure. Checking the div operator on the week number is the way of determining whether you are in week a or b. If it is before Thursday, it is quite likely that the number will be 1 less than you anticipate.
The coding looks fairly good to me, though I have not stepped through all of it. It does look correct.
Using W on consecutive Fridays, mod by 2. Both lines output 1. So doing it this way will occasionally fail.
echo date('W',strtotime('2016-01-01'))%2;
echo date('W',strtotime('2016-01-08'))%2;
Just a simple way.
<?php
$weekNumber = date("W");
echo 'Week number:',$weekNumber;
if($weekNumber&1) {
echo '<strong>Week A.</strong>';
} else {
echo '<strong>Week B.</strong>';
}
?>
$day = '2019-11-10';
$date = new DateTime($day);
$dayOfMonth = $date->format("j"); // month days 1 - 30
$weekNumber = ceil($dayOfMonth / 7); // get the week number
if ($weekNumber % 2 == 0) { //if week number is even
echo "Even Week";
} else {
echo "Odd Week";
}
**// output Even Week**

Categories