hello i am using insert query with a different pattern so i just want to
insert the current date in database but i can't find a place to put $date in insert query
$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));
$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`, `userid`
FROM `shopping_cart` WHERE `userid` = $uid";
You should not pass date or datetime from PHP if you want the date/datetime at the time of yours query's execution, instead you can use built-in MySQL functions to do so
To insert current date & time use NOW()
$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, NOW()
FROM `shopping_cart` WHERE `userid` = $uid";
To insert current date only use CURRENT_DATE()
$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, CURRENT_DATE()
FROM `shopping_cart` WHERE `userid` = $uid";
These queries assume you have a column named dt in your delivery table. Please change column name if different in your schema, or add a date or datetime column in your table if you do not have any yet
You dont need to do the date capture in PHP, as long as you want the date to be the date as at time of execution of the query. You can use the MySQL NOW() function, just add it to the inner query in the correct column to match the outer queries columns.
$sql="INSERT INTO `delivery` (product_id,pr_name,pr_price,pr_size,user_id, PR_DATE)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`, `userid`, NOW()
FROM `shopping_cart` WHERE `userid` = $uid";
If you want to insert your own date then use as per below-
$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));
$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, your_date_column)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, '$date'
FROM `shopping_cart` WHERE `userid` = $uid";
If you want to put current date then use as per below-
$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, your_date_column)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, CURDATE()
FROM `shopping_cart` WHERE `userid` = $uid";
Note: If you want to insert with time then use now().
I cant understand your clearly. Normally, we insert current date like
curdate()
sysdate()
Use now() in your query to get the current date.
First of you have to check whether you have column for current date or not in your database table. If not then you have to add a column to store current date and use that column in your query.
You can use timestamp /datetime / now() function to store current date(choose one according to your requirement)
$sql="INSERT INTO `delivery` (product_id,pr_name,pr_price,pr_size,user_id, curr_date)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, timestamp
FROM `shopping_cart` WHERE `userid` = $uid";
Related
What are the mysql codes to count the number of logins per day in PHP Timeclock?
timestamp bigint(14) is used and I have no idea how to separate them by date.
How do I count the number of ROWS per day?
Here's the command to create the table info:
CREATE TABLE info (
fullname varchar(50) NOT NULL default '',
`inout` varchar(50) NOT NULL default '',
timestamp bigint(14) default NULL,
KEY fullname (fullname)
) ENGINE=MyISAM;
Sorry I'm just a newbie trying to understand php and MySQL.
Anyway, I can add either of the two in info table:
timestamp timestamp default NULL,
or logindate date default NULL,
Suppose I have this portion of the code saved in a php file, how can I modify it so date or timestamp is inserted in info table everytime a user logs in?
$time = time();
$hour = gmdate('H',$time);
$min = gmdate('i',$time);
$sec = gmdate('s',$time);
$month = gmdate('m',$time);
$day = gmdate('d',$time);
$year = gmdate('Y',$time);
$tz_stamp = mktime ($hour, $min, $sec, $month, $day, $year);
if (strtolower($ip_logging) == "yes") {
$query = "insert into ".$db_prefix."info (fullname, `inout`, timestamp, notes, ipaddress) values ('".$fullname."', '".$inout."',
'".$tz_stamp."', '".$notes."', '".$connecting_ip."')";
} else {
$query = "insert into ".$db_prefix."info (fullname, `inout`, timestamp, notes) values ('".$fullname."', '".$inout."', '".$tz_stamp."',
'".$notes."')";
}
$result = mysql_query($query);
Since it's a BIGINT (why, btw?) I assume it's a UNIX Timestamp.
I haven't tested this, but something along the lines of this should work:
SELECT DATE(FROM_UNIXTIME(timestamp)) AS date, COUNT(*)
FROM info
GROUP BY date
You might wanna just store the timestamp as a TIMESTAMP column type, and then just use DATE(timestamp) to group by date.
Add a column of date only (without the time) and use this:
SELECT COUNT(*), dateColumn FROM info WHERE fullname='{The user full name}' GROUP BY dateColumn
Below is my table structure
Now I am in need of a query in whicn I can display the days,hours,minutes,second left for that event.
I have use this query
select title,`date`,subject ,TIMESTAMPDIFF(DAY, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'),'2013-05-25 12:00:00') as dayleft ,TIMESTAMPDIFF(HOUR, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'),'2013-05-25 12:00:00') as hourleft from jos_holidays where `date` > CONVERT_TZ(NOW(),##session.time_zone, '+05:30')
and the above results me
But my need to get exact time left includes days,hours,minutes and seconds and display it in seperate columns. Can I achive this in query? Though this can be achievable in PHP.
Also the date "2013-05-25 12:00:00" value in query is hard coded . Can be make it dynamic too.
Thanks
Query:
SQLFIDDLEExample
SELECT title,
`date`,
subject ,
TIMESTAMPDIFF(DAY, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`) AS dayleft,
TIMESTAMPDIFF(HOUR, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`)
- TIMESTAMPDIFF(DAY, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`)*24 AS hourleft,
TIMESTAMPDIFF(MINUTE, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`) -
TIMESTAMPDIFF(HOUR, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`)* 60 AS minuteleft,
TIMESTAMPDIFF(SECOND, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`)-
TIMESTAMPDIFF(MINUTE, CONVERT_TZ(NOW(),##session.time_zone, '+05:30'), `date`)*60 AS secondleft
FROM jos_holidays
WHERE `date` > CONVERT_TZ(NOW(),##session.time_zone, '+05:30')
Try like
$diff = strtotime($date) - time();
echo "tile left is ".date('Y-m-d H-m-i',$diff);
In PHP you can use
$now = new DateTime(); // or use any start date you want
$date = new DateTime($row['date']); // your sql query result
$diff = $now->diff($date);
This makes $diff a DateInterval object that contains all the information you are looking for. It does require PHP 5.3+.
I'm not sure if it would be possible to get the same information directly from MySQL. But I would assume that it's mainly for display purposes, so calculating this in PHP from available information is not too much overhead.
its work i have used this query
SELECT
TIMESTAMPDIFF(DAY,NOW(),'2013-06-23') AS dafLeft,
TIMESTAMPDIFF(HOUR,NOW(),'2013-06-23')-TIMESTAMPDIFF(DAY,NOW(),'2013-06-23')*24 AS hourLeft,
TIMESTAMPDIFF(MINUTE,NOW(),'2013-06-23')-TIMESTAMPDIFF(HOUR,NOW(),'2013-06-23')*60 AS minLeft,
TIMESTAMPDIFF(SECOND,NOW(),'2013-06-23')-TIMESTAMPDIFF(MINUTE,NOW(),'2013-06-23')*60 AS secLeft;
For dynamic just '2013-06-23' to your field name (date field name) and fetch like you do in other table.
Happy coding!! :)
I'm writing a PHP/MySQL program. I need to add the current date each time a new record to the TABLE is added. I have a field called DATE_ADDED. I'm confused how to use CURDATE() function from MySQL to write this to the TABLE. Or should I from PHP use a date function to get today's date and pass it as a variable to be written to the TABLE? I don't need a TIMESTAMP, just YYYY-MM-DD.
Thanks!
You have to try with php like
$today = date("Y-m-d");
and then while adding data to your db give this value with the remaining
INSERT INTO table_name ( field1, field2,...DATE_ADDED )
VALUES
( value1, value2,...CURDATE());
You can set it as the default value for that column date_added in the table definition like so:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
$regdate=date('Y-m-d');
$sql = "INSERT INTO table_name (fld_name1, fld_name2,fld_regdate) VALUES ('value1', 'value2', '$regdate')";
$rs = mysql_query($sql) or die('ERROR:' mysql_error());
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
I want to do something which is not complicated but I do not manage to succeed even after I tried a lot of things....
First of all, I have a database mysql with a row of timestamp type. I insert into it elements with a date like this:
$date = date('Y-m-d H:i:s', time());
$req =mysql_query("INSERT INTO my_table (id, departement, voie, date,message)
VALUES ('', '$departement_token', '$voie_token','$date' , '$message_token')");
The result of this code line is a date element like this : 2012-07-19 20:18:17
I want to delete all elements with a date > current date + 20 minutes and I do not succeed...
I tried this:
mysql_query("DELETE FROM my_table WHERE DATE_SUB(date,INTERVAL 20 MINUTE) ORDER BY date");
And this:
$req=mysql_query("DELETE FROM my_table WHERE date >= TIMESTAMPADD(MINUTE,-20,NOW())
ORDER BY date");
And this:
$timePlus20min = time() + 1200;
//et on compare les deux dates
$req = mysql_query(
"DELETE FROM my_table WHERE UNIX_TIMESTAMP(date) >= '$timePlus20min' ORDER BY date");
But none of this works. Could you help me please, I think it's not too difficult but I'm out of ideas...
$period=date('Y-m-d H:i:s',time()-(60*60*6)); // 6 hour before
"SELECT * FROM limitlessisa WHERE UNIX_TIMESTAMP(REG) > UNIX_TIMESTAMP('$period')";
REG // timestamp row name
This is working. 6 hours before listing data
In all your queries you forgot the WHERE keyword so they are syntactically wrong. Also, the ORDER BY statement has no effect as you are not executing a SELECT query. There is no result that could be ordered. Instead, a single table DELETE query returns a count of the number of deleted rows.
DELETE FROM my_table WHERE date >= DATE_ADD(NOW(), INTERVAL 20 MINUTE);
As I do not know your table structure, I tried this query with the following table:
CREATE TABLE `datetest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
try this :
$req=mysql_query("DELETE FROM my_table WHERE date <= ".date("Y-m-d H:i:s", mktime(date("H"), date("i")-20, date("s"), date("m"), date("d"), date("Y")));
Bonne chance à toi / Good luck
Try this:
DELETE FROM my_table WHERE UNIX_TIMESTAMP(date) > 60 * 60 * 20 + UNIX_TIMESTAMP();
Well I have a task to store "quotes" into a database (Already done this) and display them & sort them out for the most recent quotes. I'm assuming to get the "most recent", I'd need to store date/time of the submitted quote.
I am new to PHP and trying to learn, so I don't know how to exactly do this.
Here is the PHP for adding the quotes to the database. There are two columns in the table called "quotes" and "id". I'm guessing I will also need to make a column for the date too?
require('includes/connect.php');
$quote = $_POST['quote'];
$quotes = mysql_real_escape_string($quote);
mysql_query("INSERT INTO entries (quote) VALUES('$quotes')")
or die(mysql_error());
How would I also insert the date?
use CURDATE() if you want to insert the current date
example:
$query_auto = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";
but if you wqant it manually then should use this:
$query_manual = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-07-04')";
UPDATE
CREATE TABLE auto_ins
(
`MySQL_Function` VARCHAR(30),
`DateTime` DATETIME,
`Date` DATE,
`Time` TIME,
`Year` YEAR,
`TimeStamp` TIMESTAMP
);
INSERT INTO auto_ins
(`MySQL_Function`, `DateTime`, `Date`, `Time`, `Year`, `TimeStamp`)
VALUES
(“CURDATE()”, CURDATE(), CURDATE(), CURDATE(), CURDATE(), CURDATE());
If you only want the most recent quotes, you can simply sort your result set by their id DESC assuming the id is an auto-incremented value.
Yes, you need a third column lets say most_recent (defined as date or datetime) :
mysql_query("INSERT INTO entries (quote, most_recent) VALUES('$quotes', now())")
You will need at least couple of tables who submitted the quote and the quote table itself.
create table users(id int primary key not null, username varchar(32),pwd varchar(32));
you can add any info to that table like email address and so on.
create table quotes (
id int not null ,
user_id integer,
quote_text varchar(256),
inserted_date timestamp default current_timestamp ,primary key (id));
alter table quotes add constraint fk_users foreign key(user_id) references users(id);
Otherwise feel free to modify them.
It's not about php here its about DB design in general.
Use this code:
require('includes/connect.php');
$quote = $_POST['quote'];
$quotes = now().' - '.mysql_real_escape_string($quote);
// THIS WILL ADD THE DATE AND TIME TO YOUR $quotes STRING.
mysql_query("INSERT INTO entries (quote) VALUES('$quotes')")
or die(mysql_error());