I am having a form with two textboxes where i am displaying the month calender like in any other online forms to select the date. The user selects the date. I want the user to select a date which is equal to or after the system date like tomorrow. I am comparing the dates and then trying to insert it into the database with two columns opening_date and closing dates. I don't even want the textbox to be left empty. I have the below code but isn't working out.
<?php
mysql_select_db("trials");
if(isset($_POST['next']))
{
$odate=date("Y-m-d");//system date
echo $odate;
if(($_POST['date1']>$odate)||($_POST['date1']==$odate)&&($_POST['date1']!=""))
{
$s=mysql_query("insert into registration_date (opening_date, closing_date) values('$_POST[date1]','$_POST[date2]')");
echo $s;
if(mysql_query($s))
{
echo "successful";
}
else echo "error".mysql_error();
}
else
echo "Enter A Valid Date";
}
?>
You're also using mysql_query twice once on the set of $s and then your doing mysql_query($s) later on which is in effect doing mysql_query(mysql_query("sql"))
$s=mysql_query("insert into registration_date (opening_date, closing_date) values('$_POST[date1]','$_POST[date2]')");
echo $s;
if(mysql_query($s))
Replace
if(($_POST['date1']>$odate)||($_POST['date1]==$odate)&&($_POST['date1']!=""))
with this
if(($_POST['date1']>$odate)||($_POST['date1']==$odate)&&($_POST['date1']!=""))
You have missed one quote for date1 and also try like
$open_date = $_POST['date1'];
$close_date = $_POST['date2'];
$s=mysql_query("INSERT INTO registration_date (opening_date, closing_date)
VALUES ('$open_date','$close_date')");
And try to avoid using mysql_* functions due to they are depricated.Instead use mysqli_* functions or PDO statements
get system date with function time() like this: $odate=time(); and trasform $_POST[date] in a number like this: strtotime($_post[date]). After this you can do this: $_POST[date]>$odate. in this way you will have two numbers to compare.
Related
i'm saving time for first login ,now when user logs in i enter time using NOW() function, that saves time in this format (data type is DATETIME.
2015-12-24 15:47:30
Now logic is like every login is first login so i've to check if there already exists an entry for today to check that i fetch time explode it and get time like this
$logintime= mysqli_query($connection,"SELECT loggedin from employees");
$loggedin_time= mysqli_fetch_assoc($logintime);
$Date = $loggedin_time['loggedin'];
$loggedin_time_converted= explode(" ",$yourDate) ;
$ConvertedDate = $loggedin_time_converted[0];
last line returns 2015-12-24 now i've date
$today= time();
$DateToday= date("Y-m-d",$today);
$DateToday also returns me same format and same date now i need your help me to compare these dates , if they are equel i dont uopdate database if they are not i will , Pleas help me how do i compare these values
You can do the test in MySQL
$result = mysqli_query($connection, "SELECT DATE(loggedin) = CURDATE() AS logged_in_today FROM employees");
$row = mysqli_fetch_assoc($result);
if (!$row['logged_in_today']) {
// code to update database
}
Wow, you've done all the hard stuff to get the problem to the point of being a simple comparison of 2 strings. This is all you need to do to get over the finish line ...
if ($ConvertedDate !== $DateToday) {
// update the database
}
You can use Php Built In function "Date Difference."
Code Seems Like As Follow:-
$today= time();
$DateToday= date("Y-m-d",$today);
$diff = date_diff($today,$DateToday);
echo "$diff days";
This will return values something like +12 days or something else.
I have some data in my table which are [name][address][phone_number] and the date in this format 2015-10-14 14:37:38. I am using php PDO. How can I query out just today date from the table?
The following code is my code to query out result for the past 7 days which worked perfectly. However, whenever I replace it with 1 it doesn't work:
$query = $digital->query('SELECT * FROM sales WHERE `datetime` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() ORDER BY sale_id DESC');
What I want is to be able to query out all today data inserted into database.
You can use this, haven't tested.
<?php
$todaysDate = date('Y-m-d'); //if your date is stored in y-m-d format
try
{
$s = $conn->query("SELECT * from sales where datetime LIKE '%$todaysDate%'");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$results = $s->fetch(PDO::FETCH_OBJ);
?>
Your query results is now in $results variable.
Extended version
<?php
try
{
$s = $conn->query("SELECT * from sales");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
while($results = $s->fetch(PDO::FETCH_OBJ))
{
$date = explode(" ", $results->datetime);
if($date[0] == date('Y-m-d'))
{
//write code here to display data
}
}
?>
Make sure you replace all the columnNames and tablename.
Edit :-
Here's a sqlfiddle pertaining to my first solution.
http://sqlfiddle.com/#!9/7c2f1/3
I dont know whether it applies to PDO, as I'm not very acquainted to it, but I use to pass the date in a var, then ask for a match in my sql statement
// choose your own timezone here
date_default_timezone_set('America/Sao_Paulo');
// then define your variable as the current time:
$date = date("Y-m-d H:i:s");
then, i'd use the correct PDO syntax to compare the column with the var (a simple "where" statement should do it).
(using codeigniter syntax)
$this->db->where('date', $date);
I need to create a script that compares one field in the database (has a date stored, it's type is "TEXT" and cannot be changed DATE) to the current server date.
The dates are encoded like this "1380571547", so i need to use strftime() to decode them. This field for example, decoded with strftime corresponds to this "Sep-30-2013, 22:05"
What I need is to compare those fields with the current date, and according to that condition, write something like "Expired" in another field.
To achieve this, I made this block of code:
<?php
require("connection.php");
$today = strftime('%b-%d-%Y, %H:%M');
$exp_date = mysql_query("SELECT numbers FROM date");
while($row = mysql_fetch_array($exp_date))
{
echo (strftime ( '%b-%d-%Y, %H:%M', $row ['numbers'])). "<br />";
}
if ($exp_date < $today) {
$sql = "INSERT INTO date (changed) VALUES ('EXPIRED')";
$result = mysql_query($sql);
echo "ADDED!";
}
?>
However, this code is not working, can someone help me ?
PHP is not my strong point but it looks to me like you condition is doing a comparison on an array,
IE:
if ($exp_date < $today) // will always be false.
Your code would probably have to look something more like this.
while($row = mysql_fetch_array($exp_date))
{
if ($row[0] < $today)
{
$sql = "Update date set changed = VALUE where rowid = rowid";
$result = mysql_query($sql);
echo "ADDED!";
}
}
having said that i would probably do the comparison and update in SQL using a case statement,
Update Date
set changed = case when number > ExpiryDate
then "Expired"
else "Current"
end
You can do all this in a single query:
UPDATE `date` set `changed`='Expired' where date(now()) > date(from_unixtime(`numbers`))
But this is not what your code is attempting to do. Your second block seems to be inserting the word Expired in new rows, rather than updating anything.
Note that the table name date should be wrapped in backticks to avoid any possible clash with MySQL keywords
I don't understand the second block of code with the insert. I would do an update inside the loop. but if your going to do that, it could probably be done in one combined update statement.
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.
Trying to figure out why my code isn't working. Basically I have an elseif statment like so:
mysql_connect("localhost","xxxx","xxxxx");
mysql_select_db("xxxxxx");
$sql = "SELECT COUNT(DATE) FROM calendar";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$checkdate = $row['DATE'];
$DATEFROM = $_POST['DATEFROM'];
$DAYCOUNT = $_POST['DAYCOUNT'];
$DAYS = $_POST['DAYS'];
if ( $DAYCOUNT < $DAYS ) {
header( 'Location: request_go_fail.php' );
}
else if ( $checkdate == $DATEFROM ) {
echo "FAIL!";
}
else {
It doesn't work, the first check (to see if the DAYCOUNT is less than DAYS works fine, but when comparing to entries in the DB it doesn't seem to do it. Seems to be some issue with finding the already existing data, as when I change $checkdate to an entry that's already in the database it works great.
Any help is most appreciated :)
SELECT COUNT(DATE) FROM calendar doesn't return a field called date, print_r the $row variable to confirm that. Best solution is to change the statement to something like SELECT COUNT(DATE) AS datecount FROM calendar and then do $checkdate = $row['datecount'];
But while rereading your code fragment, I'm not sure that you really want the count of DATE's in the calendar table, and what exactly the intention is, is hard to determine from the code fragment.
Also, DATE is a reserved word in SQL, not the optimal choice for a column name!
Did you try printing $checkdate? I suspect it's null if that is indeed the SQL you're using.
Should be $row['COUNT(DATE)'] I believe, or you can use mysql_fetch_array and $row[0] instead, or use an AS in your SQL or
$checkdate = mysql_result($result, 0);
And skip the fetch call all together.
COUNT(DATE) will return the number of non-null DATE fields in your DB btw, is that really what you want?
You don't have a DATE key in the $row variable because of the sql command. Use this instead, it's called Alias:
SELECT COUNT(DATE) AS DATE_COUNT FROM calendar
Now you have a key DATE_COUNT which will contains value.
$checkdate = $row['DATE_COUNT'];