I have different events wherein users can sign-up. These events have their own date and time schedules. How do I add a sort of a time restriction wherein it will automatically restrict the users from signing up?
For Example, I have an event scheduled on March 10, 2012. Users will only be allowed to sign-up for the event up to March 9, 2012.
My table for the events contains the following fields:
eventID/eventName/eventTime/eventDate/eventCapacity/eventDescription
Thanks for your help!
Get event using eventTime/eventDate parameter and check today's date is smaller or not using datediff function.if date is bigger then reuse to accept.
Retrieve the event date and the current date and calculate the difference using PHP date object
mysql_connect("localhost", "root", "");
mysql_select_db("myDb")
$query="SELECT * FROM events WHERE eventName='$eventName' ";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$currentTime= new DateTime("now UTC");
$eventTime=new DateTime($row['thisEventDate']);
$interval=$eventTime->diff($currentTime);
$days=$interval->format('%d');
if($days<=1) {
print "sorry too late, we're sold out";
}
else {
//allow for registration fields.
}
By comparing the current date and desired date or taking the difference or current date and desired date, and accordingly displaying the HTML form into the PHP if loop
Sample
if($days<=1) {
echo "where were you wen i asked you to do so!!! ";
}
else {
<form>
field1
field2
...
</form>
}
here, $days is difference of dates
Related
I have in my MSSQL database a column with datatype of datetime which contains some dates in this format 2021-01-11 19:58:04.277.
This is a voting system, the idea is that the users can only vote once every 24 hours.
Every time they vote this table is updated with a new record and a new date is added with the corresponding user.
I want to display a message that says how many hours left to place the next vote.
This is the code I am trying to use:
/**
* Get Votes Time
*
*/
public function getVoteRemainingTime($account) {
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$sql = "SELECT VoteDate FROM dbo.vote WHERE Account = :account ORDER BY logid DESC";
$query = $this->db->prepare($sql);
$query->execute(array(':account' => $account));
$voteDate = $query->fetch(PDO::FETCH_OBJ);
$timeLeftVote = strtotime($currentTime) - strtotime($voteDate->VoteDate);
if($timeLeftVote > 86400) {
return '<strong>Vote Available!</strong>';
} else {
return $timeLeftVote;
}
}
But it is displaying the wrong information. What I am doing wrong? I would appreciate your help.
Thanks!
you need declare format parameter of the date() like date('Y-m-d H:i:s')
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$timeLeftVote = strtotime($currentTime) - strtotime('2021-01-11 19:58:04.277');
if($timeLeftVote > 86400){
echo 'Vote available';
}else{
echo $timeLeftVote;
}
Instead of SELECT VoteDate FROM dbo.vote
Can you do the calculation on the time difference at source in the database using
SELECT VoteDate, DATEDIFF(HOUR, VoteDate, GETDATE()) as HourDifference from dbo.vote
As I cannot check your database query, I only checked the rest of the code and it seems to work (as Fikri F mentioned in the comments of this post) if I replace $voteDate->VoteDate by a static date.
So please provide more information. You could output the current time and the previous vote time from the database as strings, and for both dates as well the result of strtotime, and in the end the result of the method. Then please explain, what the wrong behaviour is. By this, we can narrow down the problem either to the DB query or to the PHP code.
(I would write this as a comment, but I have not enough reputation.)
I have searched and all questions are related to the comparison being in the query.
I'm building an e-commerce website and I have a feature that allows the customer to return products 7 days upon date of purchase.
So I have, a column in mysql that tells when they bought. And I have to compare the current time (Philippines) to the mysql column, so the system can tell if they will have the option allowed.
The logic I'm thinking of but can't find the right syntax:
(inside the while loop that fetches columns as arrays, my variable is $info)
$php_current_datetime = date_now_in_php_format;
$purchase_datetime = $info['purchase_date'];
$expire_date = $info['purchase_date'] + 168 hours;
Then compare it
if($php_current_datetime < $expire_date){
echo "show the option";
}
else{
echo "already expired";
}
I would recommend making all the values timestamps and calculate the difference between them in seconds.
$secDifference = 60*60*168;
$dtObject= new DateTime($mysqlDatetime);
if ($dtObject->getTimestamp() > time()-$secDifference) {
//show the option
}
I have a database that holds jobs. It holds the job name and the expiration date. My database is user_job(id, user_id, job_name, day, month, year). The form that used in order to insert expiration date for the job in database included 3 drop-down lists. One that the user selected day(values 1-31) then month(values jan to dec) and year(2014 to 2024). I use the following function to get server's date:
<?php
$server_date = date('Y-m-d');
$a = mysql_query("select * from `user_job` where `user_id`='$session_user_id' ");
while($run_job = mysql_fetch_array($a)){
$the_job_day = $run_job['day'];
$the_job_month = $run_job['month'];
$the_job_year = $run_job['year'];
}
?>
My question is if there is a possibility now to compare server's date with the job's expiration date in my database. And if expiration date has passed just to echo a message, "expired". Is there a posibility to do this?
$the_job_time = strtotime($the_job_year .'-'. $the_job_month .'-'. $the_job_day);
$current_time = time();
if ($current_time > $the_job_time) {
// the job have expired
}
But I would suggest to store the time differently in the database. There are a bunch of different date/time types you can use
There is more than one way to do it, by order of effectiveness :
Store your date as a date column in you database, then use a select statement to compare with NOW()
Get the current timestamps and the timestamps of you job, then compare them.
Use the DateTime class of PHP to use the DateTime::diff() method (see the documentation)
I'm using JQuery datepicker to allow user select a day and book something.
then I save the date in database,
later on I get the dates out of database and disable those days in datapicker so next user can't select the booked days.
so far so good.
when a user select the day they want to book I need to disable the day after too.
I'm using php to save the chosen date in database together with some other information about user.
then i use ajax to load the dates thru php again and disable them.
how can i disable the they after? do I do it with php or javascript?
I have no idea how to get the day after selected date and how to save it? or should i save it or just add it later on when I get back the first date from ajax call and then loop thru the dates and get the day after with some function?
I would use PHP if you can by simply returning a list of dates to the beforeShowDay method of your DatePicker.
When you are adding a date from the database to the list, also use strtotime($Date, '1 day') to add the next day.
Edit:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT distinct Date, DATE_ADD(Date, INTERVAL 31 DAY) as Tomorrow from Table";
$result = $mysqli->query($query);
$Dates = array();
/* numeric array */
while(($row = $result->fetch_array(MYSQLI_NUM)) !== null)
{
//$Tomorrow = strtotime('1 day', $row['Date']);
$Tomorrow = $row['Tomorrow'];
if(!in_array($row['Date'], $Dates)) $Dates[] = $row['Date'];
if(!in_array($Tomorrow, $Dates)) $Dates[] = $Tomorrow;
}
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
header('content-type: application/json');
die(json_encode(array('Dates' => $Dates)));
You can use PHPs strtotime() function:
strtotime("+1 day", $reservationDate)
You can call any javascript function onSelect of datepicker
for just an example
onSelect: function(dateText, inst){
//dateFormat: 'DD, MM d, yy'
var theDate = $(inst).datepicker.formatDate('DD, MM d, yy', $(inst).datepicker.('getDate'));
$("#temp_date_start").text(theDate);
}
If I would have to solve this problem. I would do this by sending one date to PHP (from JavaScript) and then use PHP to add both dates to the database. So I would use the PHP date functionality to get the next day (for example with http://www.php.net/manual/en/datetime.add.php). This because working with dates in JQuery isn't as easy as in PHP, but it's not because it isn't easy that it is impossible of course :-)
But if you want to use the functionality in real time. So for example if you don't have a page refresh between clicking on the selected date and disabling those dates. In that case I would suggest that your PHP background call returns the dates to disable (or the ids of the elements or something like that).
tl;dr: Send one date to PHP, use PHP date functions to get next day, save both dates in your database
$("#elementID").datepicker({
onSelect: function(selectedDate) {
var minDate = $(this).datepicker('getDate'); //get selectedDate
minDate.setDate(minDate.getDate()+1); //adds one day to minDate
//do whatever you do to disable the date using "minDate"
}
});
I've searched through the posts for an answer and the closest answer came from the topic "trying-to-post-date-via-php-with-mysql-need-help"
I've got a form with 2 input fields and .The user selects the date from an html5 popup calendar picker, types in the time, and submits the form to a mysql db. I'm having trouble getting the date and time fields to validate/upload to the db.
In my MySQL db I have a table called appointments with a column called curdate that has a type of DATE; another column is called curtime with a type of TIME.
My PHP file has the PHP validation and SQL actions at the top of the file and the sticky html form at the bottom. The form uses to send the form
Here is the code:
$td = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $td );
if(isset($_POST['submitted'])) {
require_once('mysqli_connect.php');
$errors=array();
if(empty($_POST['curdate'])) {
$errors[]='Your date did not match';
} else {
$td=mysqli_real_escape_string($dbc,trim($_POST['curdate']));
}
if(empty($_POST['curtime'])) {
$errors[]='Your time is empty';
} else {
$tt=$_POST['curtime'];
}
if(empty($errors)) {
$q="INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')";
$r=#mysqli_query($dbc,$q);
if($r) {
echo '<h1>Thanks</h1>
<p>You are now registered</p><p><br/></p>';
} else {
echo '<h1>System Error</h1>
<p class="error">You could not be registered</p>';
echo '<p>'.mysqli_error($dbc).'<br/><br/>Query:'.$q.'</p>';
}
mysqli_close();
include('includes/footer.html');
exit();
} else {
echo '<h1>Error</h1>
<p class="error">The following errors occurred:<br/>';
foreach ($errors as $msg) {
echo "-$msg<br/>\n";
}
echo '</p><p> Please try again</p><p><br/></p>';
}
mysqli_close($dbc);
}
Mysql Or Mysqli Supports date format as 'yyyy-mm-dd' so u need to convert date and time in proper format before inserting in DB
If you want to insert current date and time when the user submits the form then Use following Query
INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason)
VALUES ('$fn','$ln','$e',CURDATE(),CURTIME(),'$phys','$reason')
But if you want to insert date from form field then do following
$td=$_POST['curdate'];
$tt=$_POST['curtime'];
$td=date('Y-m-d',strtotime($td)); //Converts date to 'yyyy-mm-dd' acceptable to mysql
$tt=date('H:i:s',strtotime($tt)); //converts time to HH:mm:ss
//Now apply Your Query as
INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason)
VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')
You might have a problem, if you in mysql made that row in table dateandtime type, if that's the case you won't be able to store it like that. If you want it to be like that just use text or varchar ....
DATE - Stores a date value in the form YYYY-MM-DD. For example 2008-10-23.
DATETIME - Stores a date and time value of the form YYYY-MM-DD HH:MM:SS. For example 2008-10-23 10:37:22. The supported range of dates and times is 1000-01-01 00:00:00 all the way through to 9999-12-31 23:59:59
TIMESTAMP - Similar to DATETIME with some differences depending on the version of MySQL and the mode in which the server is running.