I'm trying to pass a date parameter to a php file so it could insert it into a MySQL table. This is how the date is being transferred:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss%20yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());
And this is the part in the php file that tuches the date:
$date = $_GET['dt'];
$dateFirst = substr($date, 0, 8);
$dateSecond = substr($date, 11, 10);
$dateComplete = $dateFirst . ' ' . $dateSecond;
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
The date field in the table is dateTime. The procedure doesn't fall but the date in the table looks like this: "00:00:00 0000-00-00". Can someone please tell me what am I doing wrong? Thank you in advance!
Switch the order of the concatenation:
$dateComplete = $dateSecond . ' ' . $dateFirst;
The way it is currently written you will be passing '11:07:19 2014-07-09' which is not a date format the MySQL understands.
You can use the mysql function NOW() or just insert the formatted php date
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, NOW())");
OR
$dateComplete = date('Y-m-d H:i:s');
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
Related
This question already has an answer here:
Mysql saved my date column as 0000-00-00?
(1 answer)
Closed 6 years ago.
I am trying to add date and time when an account is created when submitting the registration form. I am trying this and it delivers: 0000-00-00 00:00:00
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', now())") or die(mysql_error());
This is completely different than the duplicate answered question you are replacing it with..
$date = date('Y-m-d H:i:s');
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '$date')") or die(mysql_error());
Is it your fields DATETIME OR TIMESTAMP?, be sure that it is one of them.
Also, if your field is TIMESTAMP you can set the default value as CURRENT_TIMESTAMP
Before using now() function, make sure you have set your timezone and check by echoing wat it is printing before putting it into query directly.
//insert raw time stamp into database
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '" . time() . "')") or die(mysql_error());
//get time stamp from database and format it
$user = mysql_query("SELECT * FROM users where id = '1'");
if (mysql_num_rows($user)>0) {
$row = mysql_fetch_array($user);
echo 'Created: ', date('F jS Y - g:i a', $row['created']);
} else {
echo 'User doesn\'t exist';
}
Take a look into PHP's date function, it takes 2 parameters. First being the format you want, second being the UNIX time stamp.
So if you enter the raw time stamp into the database, you can retrieve it as that and format it how you like when retrieving it.
On another note, take a look into PDO. mysql_* functions will soon be deprecated.
I want to write script function to insert multiple same query.
I want to insert multiple same records to table for example I want to insert it ten times but with different date for each insertion .
mysql_query("INSERT INTO `table` (name,age,address,phone,dat)
VALUES ('$name','$email','$address','$phone','$date')")
or die (mysql_ereror());
mysql_query("
INSERT INTO `table` (name,age,address,phone,dat)
VALUES
('$name','$email','$address','$phone','$date1'),
('$name','$email','$address','$phone','$date2'),
('$name','$email','$address','$phone','$date3'),
('$name','$email','$address','$phone','$date4'),
('$name','$email','$address','$phone','$dateN')
")
or die (mysql_ereror());
http://dev.mysql.com/doc/refman/5.6/en/insert.html
Try this code.
$date1 = date_create("2015-09-12");
$date2 = date_create("2015-09-20");
$interval = date_diff($date1, $date2);
$dateDifference = $interval->format('%a days');
$query = "INSERT INTO `table` (name,age,address,phone,dat) VALUES ";
for($i=0; $i<=$dateDifference;$i++)
{
$date = date_create("2015-09-12");
$newDate = date_add($date, date_interval_create_from_date_string($i.' days'));
$query .= "('$name', '$age', '$address', '$phone', '".date_format($newDate, 'd-m-Y')."'), ";
}
$query = substr($query, 0, -2);
mysql_query($query);
This works...
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', now())
")
This doesn't work....
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', date('Y-m-d H:i:s', strtotime('+5 hour')))
")
The mydate column in MySQL is of datetime format.
Any ideas why it's not working? I'm currently using now(), but I want it to show the time in my timezone, not the server's timezone.
I'd suggest storing the date in a variable first.
$date = date('Y-m-d H:i:s', strtotime('+5 hour'));
If both your MySQL and PHP servers are operating on the same time-zone and have their clocks properly synchronized, you wont have an issue.
and then execute the query like so:
mysqli_query($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '$date')
")
I hope this helps!
strtotime() is not MySQL function, its PHP function, you need to write it for being executes...
"INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '".date('Y-m-d H:i:s', strtotime('+5 hour'))."'
How do I change this so the time is GMT the date comes up right but time is wrong.
Any help would be grateful,Thanks
function format_mysql_datetime($raw) {
$format = '%Y-%m-%d %H:%M:%S';
$stringtime = strtotime($raw);
return strftime($format, $stringtime);
}
$raw = date('c');
mysql_query("INSERT INTO `Cars` VALUES ('', 'Pending', '$price', '{$fetchAccount['email']}', '$raw', '$content', '$description', '$area', '$town', '$UK', '$target', '0')") ;
The problem is with the timezone
use this
date_default_timezone_set('GMT');
I have used following query..
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), mktime())")or die(mysql_error());
ERROR : FUNCTION mydb.mktime does not exist
You should do this:
...'$mname', '$lname', now(), '".mktime()."')")or die(...
since now() is a valid MYSQL function, but mktime() is a php function, which is not evaluated in string context.
If you're trying to utilise the php mktime() function, you've enclosed the function call in the DB string, so the DB is attempting to call the function (which doesn't exist to the DB).
Simply remove the function call from the string body.
Try NOW(), there is no mktime function in MySQL
There are no mktime mysql function
If you want to use PHP mktime() you should not qoute it
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), ".mktime().")")or die(mysql_error());
Besides, use time() instead of mktime() without parametrs
$query1= mysql_query("INSERT into pg(pgmail, pgpass, pgfname, pgmname, pglname, updt, dtcreate) values('$email','$pass', '$fname', '$mname', '$lname', now(), " . mktime() . ")")or die(mysql_error());