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!! :)
Related
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";
I know there are several question about this error, also for php.
I've tried most of the answers and nothing seems to work.
This is my query :
"select
id,
employeenr,
name,
section,
description,
duration,
to_date(substr(startdate,0,12)||' '||starttime, 'DD-MM-YYYY HH24:MI:SS') starttime,
to_date(substr(enddate,0,12)||' '||endtime, 'DD-MM-YYYY HH24:MI:SS') endtime,
worktime,
statement,
remark
from
data
where 1=1
".$vw."
AND startdate between to_date(':df 00:00:00', 'DD-MM-YYYY HH24:MI:SS') and to_date(':dt 23:59:59', 'DD-MM-YYYY HH24:MI:SS')
".$vw1."
order by nvl(employeenr,0), startdate";
$parameters = array(':df' => $date_from, ':dt' => $date_to);
Echo var vw:
AND ( employeenr between '100000' and '199999'OR employeenr between '400000' and '499999'OR employeenr between '700000' and '799999'OR (employeenr is not null AND employeenr between '100000' and '199999'OR employeenr between '400000' and '499999'OR employeenr between '700000' and '799999'))
$date_from and $date_to:
$date_from = $_POST["date_from"]; // jQuery datepicker value
$date_to = $_POST["date_to"]; // jQuery datepicker value
What am I doing wrong?
How is $Parameters used later? It looks as if you want to use it as a bind varibale array to pass later to Oracle. If so, then you use this incorrectly, because rather than holding a variable's value the string contains SQL. This is not how bind variables work in Oracle.
You must give Oracle a complete query to parse, only that values in the query can be unknown (a date, a number or a string). These you can pass later. You cannot parse half a query and add more SQL in a variable later.
So AND ? cannot be parsed, as no value makes sense here. Something like AND X = ? could be parsed on the other hand, as Oracle would expect to get the value to compare column X with later when the query gets executed.
New question, new answer.
Still you don't use your bind variable for a variable (date, number or string), but try to replace part of a string with it. This doesn't work.
':df 00:00:00' is a string containing a colon followed by a 'd', an 'f', etc.
Use a bind variable for a complete string, i.e.:
AND startdate >= to_date(:date_from, 'DD-MM-YYYY')
AND startdate < to_date(:date_to, 'DD-MM-YYYY') + 1
or
AND TRUNC(startdate) BETWEEN to_date(:date_from, 'DD-MM-YYYY')
AND to_date(:date_to, 'DD-MM-YYYY')
or
AND startdate between to_date(:date_from || ' 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
and to_date(:date_to || ' 23:59:59', 'DD-MM-YYYY HH24:MI:SS')
SOLUTION
I have been able to fix my problem by stripping out the substr in the SELECT area of the query.
$sql_report = "
select
id,
employeenr,
name,
section,
description,
duration,
startdate,
starttime,
enddate,
endtime,
worktime,
statement,
remark
from
data
where 1=1
".$vw."
AND startdate BETWEEN to_date('$date_from', 'DD-MM-YYYY') AND to_date('$date_to', 'DD-MM-YYYY')
".$vw1."
order by nvl(employeenr,0), startdate";
$parameters = array();
I can't believe I can't do this, but I want to be able to store the current date and time from php in to a mysql table.
The column in the table is type datetime.
I've tried this
$current_date = date("Y-m-d");
$my_date = strtotime($current_date);
INSERT INTO my_table (date_time) VALUES ('$my_date')
but my timestamp comes up as 0000-00-00 00:00:00
This must be so easy to do but I just can't get it working!
I want to use the timestamp from php rather than using the mysql now() function
Try this:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
In the date-format parameter of the date function, use :
'H' for 24hr format
'h' for 12hr format
Don't save it as the Unix Timestamp (which strtotime() outputs), but as "2012-12-02 13:00" into the DATETIME column.
Create column type TIMESTAMP and set it to NOT NULL. Then pass in NULL during INSERT and MySQL will insert current date and time. This works for me.
set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (`date_time`) VALUES (CURRENT_TIMESTAMP)
If you have the date in PHP as a timestamp, you can use the FROM_UNIXTIME function [1]
mysql> insert into table_name values (FROM_UNIXTIME(your_timestamp_here));
Hope it helped
[1]. https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
Remove the strtotime()
$current_date = date("Y-m-d");
INSERT INTO my_table (date_time) VALUES ('$current_date')
If you want to include the hour, minutes and seconds,
$current_date = date("Y-m-d H:i:s");
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();
I have table in mysql database which have field with datatype is datetime.
I want to save the datetime in this minute, for this I use " Now() ", but it does not work,
It just save 000000000000 in databaes.
If you use php, the correct format is:
date("Y-m-d H:i:s");
UPDATE:
Minutes are expressed as i not m
If you've got a timestamp in PHP check out FROM_UNIXTIME()
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
$tstamp = time();
$query = "INSERT INTO `table` VALUES (FROM_UNIXTIME($tstamp))";
INSERT ... (Now(), ...)
without additional quotes around the function Now()
I would use function time() too, then it's easy to output different kind of timestamps with date().
$query = "INSERT INTO tbl VALUES (".time().");";
date("g") this will return 12-hour format of an hour without leading zeros. For more options see http://php.net/manual/en/function.date.php
An example of a PHP script which sets a date in MySQL manually,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>
An example of a PHP script which sets a date in MySQL Automatic,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>
try this: set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (date_time) VALUES (CURRENT_TIMESTAMP)