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.
Related
I'm working on a plugin, but I can't get it working.
The code below is supposed to do something when the current date is between 2 chosen dates by the user.
So if date is in between 12-01-2016 ($snow['period_past']) and tomorrow is 12-03-2016 ($snow['period_future']), do something...
$date = date('Y-m-d');
$date = date('Y-m-d', strtotime($date));
$snowStart = date('Y-m-d', strtotime($snow['period_past']));
$snowEnd = date('Y-m-d', strtotime($snow['period_future']));
if (($date > $snowStart) && ($date < $snowEnd)) {
// do this and that
}
The code above works, but it only works between the dates. How can I make it work so it also works when its at the $snow['period_past'] date and $snow['period_future'] date?
Sorry for my bad explanation, English is not my native language.
if (($date >= $snowStart) && ($date <= $snowEnd))
{
// do this and that
}
You are doing a greater than > or less than < comparison.
To get the condition to meet when the date is equal to $snow['period_past'] or $snow['period_future'], you should have the following comparison in place:
if (($date >= $snowStart) && ($date =< $snowEnd))
{
// your code here
}
I am trying to determine if a day and time are between two others, I have the following...
$currentdate = date("N h:i:s A");
This returns the day of the week as a number and then the current time in 24 hour format.
I want to check if the $currentdate is between 9am on a Friday and 9am on a Monday.
What is the best way to tackle this?
I believe this should give you what your asking for but I'm sure there are better ways to implement. So basically the time has been converted into an INT for comparing and the hours are configured not to have a leading zero hence why $timeOne and $timeTwo is shorter. I've then used an if statement to test days and time on that specific day leaving you a slot to add your code if those conditions are met.
function checkDayTime() {
$day = date(w); //0 (for Sunday) through to 6 (for Saturday)
$timeOne = 90000;
$timeTwo = 90000;//Added for easier reading
$currentTime = (int) date('Gis'); //Time as INT 00000 > 240000
if (($day == 5 && $currentTime > $timeOne) || ($day == 6 || $day == 0) || ($day == 1 && $currentTime < $timeTwo)) {
//Between those hours
return TRUE;
} else {
//Not between those hours
return FALSE;
}
}
Just removed the extra if statement as it was not needed
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.
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.';
}
How do I write a function in PHP that returns the date of delivery without Saturdays, Sundays and holidays?
For example: for same product delivery is in 3 days for others in 5 days.
If I do a command today I will receive my command in 3 days without Saturdays, Sundays and holidays.
If I do my command now "29/12/2011" for a product "3j" I will receive your command at "4/1/2011"
In the database I have just how much the day for every type product is.
For example:
prod1: 3day
prod2: 5day
prod3: 7day
I tried to do a function but failed.
This code (slightly altered) is used in production for one of my sites:
// $dt = date of shipping, $numdays = expected number of days in transit
function realDeliveryDate($dt, $numdays)
{
$holidays = array("05/30/2011","07/04/2011","09/05/2011","11/24/2011","11/25/2011","12/25/2011","12/31/2011","01/01/2012","05/28/2012","07/04/2012","09/03/2012","11/22/2012","11/23/2012","12/25/2012");
$checkday = strtotime($dt." +".$numdays." days");
// check if it's a holiday
while(in_array(date("m/d/Y",$checkday), $holidays)) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
// make sure it's not Saturday
if (date("w",$checkday) == 6) {
$checkday = strtotime(date("m/d/Y",$checkday)." +2 days");
}
// make sure it's not Sunday
if (date("w",$checkday) == 0) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
// make sure it's not another holiday
while(in_array(date("m/d/Y",$checkday), $holidays)) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
return $checkday;
}
This is a really complex subject that you can't really fix with a simple function. You are talking about business days calculation strategies and they involve a lot of thinking.
If you want to process only business days and not business hours, then it get slightly easier.
The first step is to create an array of working days or an array of non working days. For example:
//Build the days based of weekends
$nonWorkingDays = array();
foreach($iDate = 0; $iDate < 365; $iDate++){
$date = strtotime('today +'.$iDate.' day');
if(date('w', $date) == 0 || date('w', $date) == 6){
$nonWorkingDays = date('Y-m-d', $date);
}
}
//Add the holidays
$nonWorkingDays[] = '2011-12-25';
$nonWorkingDays[] = '2012-01-01';
//Determine the date of delivery
$daysToDelivery = 6;
$deliveryDate = time();
while($daysToDelivery > 0){
$deliveryDate = time() + (24*60*60);
if(!in_array(date('Y-m-d', $deliveryDate), $nonWorkingDays)){
$daysToDelivery--;
}
}
Take a look at:
http://php.net/manual/en/function.localtime.php
This will return you a weekday based on the time you provide. So if you current time is X and you need to deliver in 2 days you would do localtime(X + 2*24*3600). The resulting struct will contain the weekday which you can use to move the time forward or backward as you need.