Compare date & time and then allow access to site (PHP4) - php

Could do with some help. So I've got a site which can only be access between two dates. In my database I've got the following
Name Type Data
start_date VARCHAR(255) - 11/01/2012
start_time VARCHAR(255) - 14:32
end_date VARCHAR(255) - 13/01/2012
end_time VARCHAR(255) - 17:20
I need to only allow access to users only on the above dates. Here's the code I'm using, but it doesn't seem to work am I doing something wrong? I'm running PHP4
$current_date = strtotime(date("Y-m-d H:i:s"));
//db query and while loop here
$start_date = explode("/", $rows['start_date']);
$start_time = $rows['start_time'];
$start_date = strtotime($start_date[2] . "-" . $start_date[1] . "-" . $start_date[0] . " " . $start_time);
$end_date = explode("/", $rows['end_date']);
$end_time = $rows['end_time'];
$end_date = strtotime($end_date[2] . "-" . $end_date[1] . "-" . $end_date[0] . " " . $end_time);
if(($current_date <= $start_date) || (($current_date >= $end_date)))
{
echo "Site can only be access between" . $start_date . " and " . $end_date;
}
else
{
// redirect to the site...
}
Thanks in advance :)

Related

PHP - Is a date in the past

I'm trying to compare if a date is today, or in the past. As far as i can tell i should be able to do date1 <= today but it shows as true even if the date is in the future
I'm getting the date from the database and adding 30 days to it using
echo "End Date: " . $rowTicket['endDate'] . "<br/>";
$disableDate = ($rowTicket['endDate'] != '0000-00-00') ? date('d-m-Y', strtotime('+30 days', strtotime($rowTicket['endDate']))) : '';
And then doing the check using
echo "Is " . $disableDate . " before today's date? (" . date("d-m-Y") . ")";
if($disableDate <= date("d-m-Y")) {
//$colourTR = $red;
echo " - Yes<br/><br/>";
} else {
echo " - No<br/><br/>";
}
The output i get from the echo's is
End Date: 2021-09-01
Is 01-10-2021 before today's date? (26-10-2021) - Yes
End Date: 2021-10-15
Is 14-11-2021 before today's date? (26-10-2021) - Yes
Because you compare strings, while should compare timestamps
echo "End Date: " . $rowTicket['endDate'] . "<br/>";
$disableDate = $rowTicket['endDate'] != '0000-00-00'
? strtotime('+30 days', strtotime($rowTicket['endDate']))
: '';
$today = strtotime('today midnight');
echo "Is {$disableDate} before today's date? (" . date("d-m-Y", $today) . ")";
if($disableDate <= $today) {
//$colourTR = $red;
echo " - Yes<br/><br/>";
} else {
echo " - No<br/><br/>";
}
You are formatting your dates as strings. PHP has no way of knowing that the string '26-10-2021' should be "less than" the string '14-11-2021'.
If you format them year-first ('Y-m-d'), it will work, but better to compare the integer timestamps (output of strtotime), or DateTimeImmutable objects.

Error while inserting date and time to MYSQL

I'm getting date and time by HTTP GET and trying to write a php code to insert these to MYSQL db but getting error.
My date and time are coming like this:
date=260117
time=180205.000
$year= "20" . substr($date, 4,2);
$month= substr($date, 2,2);
$day= substr($month, 0,2);
$dateformatted= $year . "-" . $month. "-" . $day;
$hour= substr($time, 0,2) + 3;
$min = substr($time, 2,2);
$sec = substr($time, 4,2);
$timeformatted = $hour . ":" . $min . ":" . $sec;
$datetime = $dateformatted . " " . $timeformatted;
$datetime = strtotime($datetime);
My error is like that:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '21:02:05)' at line 2
SQL:
$sql = "INSERT INTO pt_position (date) VALUES ($datetime)";
Remove this line.
$datetime = strtotime($datetime);
If you run this line, the return value is PHP's DateTime type.
$datetime = $dateformatted . " " . $timeformatted;
After this code, you can run the sql query....

Setting strtotime adding a variable to amount of days

I am trying to export a certain number of days and setting it to the variable $max_future. Currently it is a set amount of days but I want the users input variable to be the number it has instead.
Currently it is set as:
$max_future = date("Y-m-d", strtotime($today . "+6 days"));
I want something like:
$exportDays = '9'; //(or whatever the user input was)
$max_future = date("Y-m-d", strtotime($today . "+$exportDays days"));
Is this possible? I appreciate any help
Try:
$max_future = date("Y-m-d", strtotime($today . "+" . $exportDays . " days"));
Something like this :
<?php
$output = 9; // you can take this value from input.
$today = date("Y-m-d");
echo date('Y-m-d', strtotime($today. ' +'.$output. ' days'));
?>
<?php
$exportDays = $_REQUEST['exportDays']; // or however you want to get it from user input
$max_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $exportDays . ' days'));

Having trouble comparing a range of dates entered in a form with records in a mySQL database

I have a table called schedule and a column called Date where the column type is date. In that column I have a range of dates, which is currently from 2012-11-01 to 2012-11-30. I have a small form where the user can enter a range of dates (input names from and to) and I want to be able to compare the range of dates with the dates currently in the database.
This is what I have:
////////////////////////////////////////////////////////
//////First set the date range that we want to use//////
////////////////////////////////////////////////////////
if(isset($_POST['from']) && ($_POST['from'] != NULL))
{
$startDate = $_POST['from'];
}
else
{
//Default date is Today
$startDate = date("Y-m-d");
}
if(isset($_POST['to']) && ($_POST['to'] != NULL))
{
$endDate = $_POST['to'];
}
else
{
//Default day is one month from today
$endDate = date("Y-m-d", strtotime("+1 month"));
}
//////////////////////////////////////////////////////////////////////////////////////
//////Next calculate the total amount of days selected above to use as a limiter//////
//////////////////////////////////////////////////////////////////////////////////////
$dayStart = strtotime($startDate);
$dayEnd = strtotime($endDate);
$total_days = abs($dayEnd - $dayStart) / 86400 +1;
echo "Start Date: " . $startDate . "<br>End Date: " . $endDate . "<br>";
echo "Day Start: " . $dayStart . "<br>Day End: " . $dayEnd . "<br>";
echo "Total Days: " . $total_days . "<br>";
////////////////////////////////////////////////////////////////////////////////////
//////Then we're going to see if the dates selected are in the schedule table//////
////////////////////////////////////////////////////////////////////////////////////
//Select all of the dates currently in the schedule table between the range selected.
$sql = ("SELECT Date FROM schedule WHERE Date BETWEEN '$startDate' AND '$endDate' LIMIT $total_days");
//Run a check on the query to make sure it worked. If it failed then print the error.
if(!$result_date_query = $mysqli->query($sql))
{
die('There was an error getting the dates from the schedule table [' . $mysqli->error . ']');
}
//Set the dates to an array for future use.
// $current_dates = $result_date_query->fetch_assoc();
//Loop through the results while a result is being returned.
while($row = $result_date_query->fetch_assoc())
{
echo "Row: " . $row['Date'] . "<br>";
echo "Start day: " . date('Y-m-d', $dayStart) . "<br>";
//Set this loop to add 1 day to the Start Date until it reaches the End Date
for($i = $dayStart; $i <= $dayEnd; $i = strtotime('+1 day', $i))
{
$date = date('Y-m-d',$i);
echo "Loop Start day: " . date('Y-m-d', $dayStart) . "<br>";
//Run a check to see if any of the dates selected are in the schedule table.
if($row['Date'] != $date)
{
echo "Current Date: " . $row['Date'] . "<br>";
echo "Date: " . $date . "<br>";
echo "It appears as though you've selected some dates that are not in the schedule database.<br>Please correct the issue and try again.";
return;
}
}
}
//Free the result so something else can use it.
$result_date_query->free();
As you can see I've added in some echo statements so I can see what is being produced. From what I can see it looks like my $row['Date'] is not incrementing and staying at the same date. I originally had it set to a variable (currently commented out) but I thought that could be causing problems.
I have created the table with dates ranging from 2012-11-01 to 2012-11-15 for testing and entered all of this php code onto phpfiddle.org but I can't get the username provided to connect.
Here is the link: PHP Fiddle
I'll be reading through the documentation to try and figure out the user connection problem in the meantime, I would really appreciate any direction or advice you can give me.

Function in code I'm debugging seems to not take into account shifts to and from DST

I'm debugging a conference room booking calendar which was put together by someone which is no longer working with me. It's been a nightmare since there were so many things wrong with it, but I'm having trouble figuring out exactly what's causing this last bug. The calendar checks to see if rooms have already been booked at certain times and they aren't having an issue showing as booked at the right time, but if someone tries to book the same room an hour or less after the room is vacated after a shift to or from DST, it shows the room as booked still.
Example:
User sees that a room is reserved for Nov. 29th from 9am-10am.
User then tries to book the room on Nov. 29th from 10:30am-12:00pm.
The calendar cancels this second request and informs the user that the room is already booked.
It should be noted that this doesn't happen any time before the shift to DST (Nov. 4th). Here's the function which determines if the room is available:
function calCheck($starttime, $endtime, $cal_name, $cat_id, $myDB, $myHost, $myUser, $myPass) {
$timezone = 'America/Denver';
date_default_timezone_set ($timezone);
$dset = new DateTime($odate, new DateTimeZone($timezone));
$dset2 = $dset->getOffset();
//$starttime = $starttime + 1;
//$endtime = $endtime - 1;
$starttime = $starttime - $dset2 + 1;
$endtime = $endtime - $dset2 - 1;
$starttime = $starttime;
$endtime = $endtime;
//echo $starttime .'</br>'. $endtime . '</br>';
$db = new myDB($myDB, $myHost, $myUser, $myPass);
$db->myDB_Connect();
//echo 'calcheck</br>';
$ck_query = 'SELECT * FROM vw_cal_chk
WHERE (stime < '. $starttime . ' AND etime > ' . $starttime . ' ) and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime < ' . $endtime . ' AND etime > ' . $endtime . ') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime >= '. $starttime . ' AND etime < ' . $endtime .') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"';
$ck_result = $db->myQuery($ck_query);
$num = mysql_num_rows($ck_result);
//echo $ck_query . '</br>' . $num;
if ($num >> 0){
$avail = 1;
} else {
$avail = 0;
}
return $avail;
}
All of my timestamps up to this point are in UTC and I notice the $odate variable is actually never instantiated anywhere, but I haven't been able to determine what value to pass it in order for the offsetting to work correctly. If I can find out what kind of date it wants, I should be able to work the rest out.
$odate is null which defaults to now giving you the current offset. What you want is the offset at the time that the schedule is being set not the offset right at this moment. It looks like your database dates are in Mountain time and that your startdate is utc and you are subtracting the offset to get back to mountain (I would think that you would add the offsets not subtract , so I might have this reversed, can you query your database and figure it out?)
In any event you should be computing your offset using $startdate not now(), try switching $startdate for $odate and see what happens. If this doesn't work try building a string from startdate and then generating a new date from that string. If nothing else outputting that string should give you a clear picture as to whether $startdate is UTC or mountain

Categories