can any one explain how i would display my table record according date, like any user of my website can see their activity of current date only...
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
i am using this but it displaying previous date result also
here is my code, i am fetching result from different tables
$SQL="select * from $tab where 1 $con ORDER BY id desc $_REQUEST[sort_mode] $con_limit";
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
gri("users","WHERE id='$data[op_id]' ","",$op_name);
gri("patient", " where id ='$data[patient_id]' order by id desc","",$patient);
$data[first_name]=$patient[first_name];
$data[last_name]=$patient[last_name];
$data[age]=$patient[age];$data[sex]=$patient[sex];
$data[mob]=$patient[mob];
$data[op_name]=$op_name[name];
$t->set_var($data);
$t->set_var(array("chk_status_$data[id]_$data[status]"=>"selected",));
$t->parse("ABlockList","AccessBlockList",true);
}
You can also use MySQL for this.
Example
select * from table_name where create_date >= NOW();
OR
SELECT * FROM table_name WHERE DATE(create_date) = CURDATE();
OR
SELECT * FROM table_name WHERE DATE(create_date) = DATE(NOW())
DATE() returns the date without time and NOW() returns the current date & time (note we’ve used the DATE() function in this query to remove the time)
Related
I have s MySQL Query where I want to pull data from my database but base it on the current month information
FROM lbs_trace_etrack WHERE (lbs_agent = '$slfirstname' AND DATE (lbs_date) = CURDATE()) ORDER BY lbs_date DESC LIMIT 0,50");
This string pulls out the information for the current day.
I have also tried the below string but get no results from it:
FROM lbs_trace_etrack WHERE (lbs_agent = '$slfirstname' AND MONTH(lbs_date) = (MONTH(NOW()) AND YEAR(lbs_date) = YEAR(NOW())
My table date format is as follow 2016-08-02
Or using PHP variables as so:
<?php
$start = date('Y-m-01'); // FIRST DAY OF CURRENT MONTH
$end = date("Y-m-t", strtotime(date("Y-m-d"))); // LAST DAY OF CURRENT MONTH
$sql = "SELECT * FROM lbs_trace_etrack WHERE lbs_agent = '".$slfirstname."' AND (lbs_date BETWEEN '".$start."' AND '".$end."')";
?>
I have done the following and it works
FROM lbs_trace_etrack WHERE lbs_agent = '$slfirstname' AND MONTH(lbs_date) = MONTH(CURDATE()) AND YEAR(lbs_date) = YEAR(CURDATE()) ORDER BY lbs_date ASC, lbs_time ASC
Thanks to all and Tijo for guidance
Assuming lbs_agent is a DATE field type as mentioned in comments, you could do this (note I am just showing the pertinent date part of your WHERE clause):
WHERE lbs_agent >= DATE_FORMAT(NOW() ,'%Y-%m-01')
It is important that you do not use a function call on the left (field definition) side of the WHERE comparison, as you will then not be able to leverage any index on that field. Doing this would require a full table scan with MySQL performing the function on this field for every row in the table such that the comparison can be made.
Feel free to use MySQL functions for the comparison value, as those would be calculated just once when the query is being planned. You would then be able to use an index on the field for quickly filtering the rows in the table that meet the criteria. From a query execution standpoint, this is basically that same as if your query has this WHERE clause:
WHERE lbs_agent >= '2016-08-01'
This is as compared to the examples in your question which would be executed as:
WHERE DATE(lbs_date) = '2016-08-03'
and
WHERE MONTH(lbs_date) = 8 AND YEAR(lbs_date) = 2016
Both of these would require full table scan since the values derived from the field are not able to be determined until the row is scanned.
You could try to extract the month, such as EXTRACT(MONTH from NOW())
you can use following code if it timestamp
MONTH(CURDATE())
I am trying to select an unixtimestamp column from my database, as readable date time:
SELECT count(*) FROM users WHERE user_by="Admin" AND expire(FROM_UNIXTIME(unixtime),'%Y-%m-%d')='2015-10-02'
Above gives me this error:
#1305 - FUNCTION database_maindb.expire does not exist
How can I select the unixtimestamp from column expire as datetime, in the format: year-month-date?
assuming your database field is called "expire":
SELECT count(*) FROM users WHERE user_by="Admin" AND FROM_UNIXTIME(expire,'%Y-%m-%d')='2015-10-02'
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime for details
You don't need to select is as the YMD date stamp, you could do something such as (using your tags)
$query = "SELECT * FROM `table`;";
$result = pdoObject->queryMethod($query);
$result= pdoObject->fetchAssocMethod($result);
$date = date("Y-m-d", $result['epochCol']);
I hope this helps.
I have a database that contains a column with type - Date. I also have a query with the date inputted as static which works fine but I would like to use todays date in the query. any recommendations?
Query :
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND OPEN_DATE ='.$db->qstr('2014-05-14');
This currently displays count of items after 2014-05-14
You could use the NOW() function that returns the current date. To avoid skewed answered by hours/minutes/seconds, you can use date to extract the date part:
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND DATE(OPEN_DATE) = DATE(NOW())';
In my code, I am trying to find items in an activities table that are within the last day. This query is not returning any results, are there any problems with it? Is there a better query?
$curday = time() - (24*3600);
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > '$curday'";
There are two choices here, you can get and format the date through PHP or use SQL language to do it. I prefer to do it within the SQL, it also allows me to use the same query in a MySQL client.
This question is essentially the same thing: MySQL SELECT last few days?
This would be the new query:
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > DATE_ADD(CURDATE(), INTERVAL -1 DAY)";
you can try with unix function 'mktime' to get value of yesterday ..
as
$curday = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
for reference
if your database will mysql only then you can extract yesterday in sql itself..
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)
one more thing if timestamp is your column name don't put this column inside single quote ..
What you can use is DATE_SUB. This can be used as follows
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > date_sub(current_date, interval 1 day)
This way you don't need to work with current date in PHP
in Informix it would be (TODAY - 1) if the column is type DATE
In mysql database i have this column called:
Name: Date
Type: datetime
I have few values in that column:
2009-01-05 01:23:35
2009-03-08 11:58:11
2009-07-06 10:09:03
How do I retrieve current date? I am using php.
in php:
<?php $today = date('Y-m-d');?>
How to write a mysql query to retrieve all today date data?
Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???
You don't need to use PHP, MySQL has a function to get the current date:
SELECT field FROM table WHERE DATE(column) = CURDATE()
Documentation: CURDATE, DATE.
If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:
$today = date('Y-m-d');
$query = mysql_query("
SELECT field FROM table WHERE DATE(column) = '$today'
");
For date time it is not usefull, instead I try this and working...
Today's Visitors!
sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');
// last_visit coloumn of type 'datetime'