For Example I have this range : 2015-06-01 to 2015-06-03.
Now I'm going to edit this date in my form. The form however checks if the range overlaps with another range (you can't enter two leaves on the same day).
Right now I use this to verify that it does not overlap with another leave:
if($startdate !== $start_db && $enddate !== $end_db){
if($startdate <= $end_db && $enddate >= $start_db){
$error[] = 'Leave Overlaps with another leave.';
}
}
The problem occurs when I want to change the range from 2015-06-01 to 2015-06-02, then it says it overlaps, because it does not know that its the same leave.
How can I check if the new range is within the range that's saved in the DB?
You have to check if each start and end date is between the other start and end date. So a a total of four comparisons.
if(
// Start date is in first date range
($startdate >= $start_db && $startdate <= $end_db)
||
// end date is in first date range
($enddate >= $start_db && $enddate <= $end_db)
){
$error[] = 'Leave Overlaps with another leave.';
}
Related
I have two times like start_time and end_time and also having a time range like a start time and a end time. I want to check start_time and end_time are in the range of start time and end time or not. How to check that.
$start_time1 = 10:15 am; //! time table start time
$end_time1 = 12:30 pm; //! time table end time
$strattime2 = 10:00 am; //! time range - start time
$endtime2 = 1:00 pm; //! time range - end time
How to resolve this problem?
Just check the boundary of the range. The query must be started on or after the range and must be ended on or before the range ends. So
function check($queryStart, $queryEnd, $rangeStart, $rangeEnd) {
return ($queryStart >= $rangeStart && $queryEnd <= $rangeEnd);
}
If you want to check whether the query is overlapping the range or not, you should check whether the query ends before the range start or the query starts after the range end.
function overlap($queryStart, $queryEnd, $rangeStart, $rangeEnd) {
return !($queryEnd < $rangeStart || $queryStart > $rangeEnd);
}
you may convert all the times to DateTime() Objects and then check the difference like so:
<?php
function startStopTimeIsWithinRange($startTime='10:15', $stopTime='12:30') {
$dateStart = new DateTime('2016-10-30 ' . $startTime); //<== IGNORE, THE DATE. NOTICE THE TIME
$dateStop = new DateTime('2016-10-30 ' . $stopTime); //<== IGNORE, THE DATE. NOTICE THE TIME
$rangeStart = new DateTime('2016-10-30 10:00'); //<== IGNORE, THE DATE. NOTICE THE TIME
$rangeStop = new DateTime('2016-10-30 13:00'); //<== IGNORE, THE DATE. NOTICE THE TIME
if($dateStart >= $rangeStart && $rangeStop >= $dateStop){
return true;
}
return false;
}
var_dump( startStopTimeIsWithinRange('10:15', '12:30') ); //<== NOTICE THE COLON (:) AND NOT DOT (.)
The idea is to play an alarm from html/php web-page via a TV hanging near the school canteen reminding students to wash their hands. The TV is used as a tabloid screen for school events and news.
The piece of code I'm using on this page is as follows:
<?php
$hour = date('G');
$day = date('1..5'); // 1..7 for Monday to Friday
if (($hour >= 11.05 && $hour <= 11.35) // 5am - 7am
|| ($hour >= 12.15 && $hour <= 12.30) // 10am - 12 noon
|| ($hour >= 21.10 && $hour <= 21.30) // 10am - 12 noon
|| ($day == 1-5) // Monday - Friday
) { ?>
<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">
<?php } ?>
This only plays it once the page is uploaded onto the server, and if it falls within the times above. Otherwise, it stays silent. And, strangely it plays only on my home PC on Chrome/IE/Mozilla and it doesn't play completely on school PCs.
Bear in mind the page auto refreshes itself every 5 min.
Would appreciate if someone gave a hint on event listeners or anything else.
There are a couple of problems with your code.
In $day = date('1..5');, '1..5' isn't a valid format for date(). That will just return the string 1..5. You want date('w').
date('G') returns the hour in 24-hour format, so $date will be a string representation of an integer. In your if condition you are comparing it to floats. Since all of the different time conditions are various decimals above the same integers, $date will never match any of them. For example, if the time is 11:30 AM, $date will be '11', which is not between 11.05 and 11.35. (Obviously, neither is '10' or '12'.)
In the last part of the if condition, you have || ($day == 1-5). There are two problems with that. First, 1-5 does not mean "between one and five"; it means "one minus five". And second, the fact that this is an additional non-nested or condition means that it negates all of the various time comparisons, because whenever any part of an or condition is true, the entire condition evaluates to true. If it was || ($day >= 1 && $day <= 5), then the if would be true all day every Monday through Friday. As it is, it's true all day only on Thursday.
I added an updated version that I think will do what you want it to (based on the comments in your code). I used some kind of strange indentation in the if condition to hopefully show the grouping a little better. Basically it needs to check the day and check a group of time conditions. Note the different format for time ('Gi'), which gets the hour and minute in a format that can be compared to integer values in the if condition.
$day = date('w');
$time = date('Gi');
if ( ($day > 0 && $day < 6) // Monday - Friday
&& ( // AND
($time >= 1105 && $time < 1135) // time range 1
|| // OR
($time >= 1215 && $time < 1230) // time range 2
|| // OR
($time >= 2110 && $time < 2130) // time range 3
)
)
echo '<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">';
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.
Im going to implement a search function in mySQL for an application Im building,
the user types [for now, later will be a calendar picker] start date and end date,
and click search
if (start day > end day), THEN error
if (start month > end month), THEN error
Ok so far, but when having into account that
if (start day > end day) && (start month < end month), THEN search
if (start Month > end Month) && (start year < end year), THEN search
I dont worry about leap years, I will loop all months to 31 days, because if the day is in the db, it will fetch it, other wise, it will go to 31 and return nothing as there is no day,
Im using varchar for my dates (no timestamp), as they are imported from json [iOS]
Ok, hope to make sense,
here the code,
<?PHP
$start = $_POST['start_date'];
$end = $_POST['end_date'];
$start_time = explode('/', $start);
$end_time = explode('/', $end);
$count_start = $start_time[0];
$count_end = $end_time[0];
$month_start = $start_time[1];
$month_end = $end_time[1];
$year_start = $start_time[2];
$year_end = $end_time[2];
function cuenta($count_start, $count_end) {
for($count_start; $count_start <= $count_end; $count_start++) {
print $count_start . "<BR>";
}
}
if (!isset($_POST['Submit1']) || ($start == "Start Date" && $end == "End Date") || ($start == "" && $end == "End Date") || ($start == "Start Date" && $end == "")
|| ($start == "" && $end == ""))
{
print ("no data yet")."<BR>";
}
if ($year_start[2] > $year_end[2]){
print ("Please make sure end date Year is equal or greater than start date Year");
}
if (($month_start > $month_end) && ($year_start <= $year_end)){
print ("Please make sure end date Month is greater than start date Month");
}
elseif (($month_start > $month_end) && ($year_start < $year_end)){
cuenta($count_start, $count_end);
}
elseif ($count_start > $count_end) {
print ("Please make sure end date Day is greater than start date Day");
}
?>
Im beggining in php, Sorry if I miss the obvious!, [that is why im asking haha]
so if im missing some other important validation for searching between a date range, plz let me know, and also plz point me in the best direction for this forest of ifs!
thanks a lot!
Its much easier to convert the dates to unix timestamps and then compare the integers. PHP has the function strtotime for this purpose. If you have the dates already split, you could use mktime.
Hi already i'm using this code from another question - which adds two extra days to endday in the case of weekends
function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
$i=1;
$dayx = strtotime($startdate);
while($i < $buisnessdays){
$day = date('N',$dayx);
$datex = date('Y-m-d',$dayx);
if($day < 6 && !in_array($datex,$holidays))$i++;
$dayx = strtotime($datex.' +1 day');
}
return date($dateformat,$dayx);
}
This function forms part of a json output which is displayed in a jquery calendar - it picks up startdate and enddate and renders it.
Is it possible to create a code that returns outputs such that when it gets to a weekend it creates an end date, skips to Monday creates a start date then continues till it reaches the original given enddate??
x = date('w');
if (x != 6) {
while (x != 6) {
//start adding days to start date
}
} else {
//create new enddate = current iteration of dates currentdate;
//then new start (add two days to it to get to monday) currentdate + 2 = newstartdate
//redo above till you get to original end date
I'm not 100% sure about what the question/function is really doing but (if I guessed correctly) here's an idea.
function add_business_days($startdate, $businessdays, $holidays, $dateformat)
{
$start = new DateTime($startdate);
$date = new DateTime($startdate);
$date->modify("+{$businessdays} weekdays");
foreach ($holidays as $holiday) {
$holiday = new DateTime($holiday);
// If holiday is a weekday and occurs within $businessdays of the $startdate
if ($holiday->format('N') < 6 && $holiday >= $start && $holiday <= $date) {
$date->modify("+1 weekday");
}
}
return $date->format($dateformat);
}
The logic basically adds $businessdays weekdays to the start date; then checks for any holidays within the date range, if any holidays do occur then the final date is incremented as appropriate.