I'm building a SaaS calendar with MySQL & PHP.
The front-end javascript calendar lib is fullcalendar(monthly view)
http://fullcalendar.io/
I have a mysql data table named events like this:
ID
ID_USER
BODY_NAME
BODY_START
BODY_END
IS_DONE
TIME_CREATED
TIME_UPDATED
TIME_DELETED
The BODY_START and BODY_END is using unix_timestamp.
Then in my PHP code, I built a query helper that generate a SQL like:
"SELECT * FROM `" . $this->_tableName . "` WHERE `ID_ACCOUNT` = '$_id_account' AND (( `BODY_START` >= 1424649600 AND `BODY_START` <= 1428278400 ) OR (`BODY_END` >= 1424649600 AND `BODY_END` <= 1428278400))";
The start and end timestamp params provided by the fullcalendar.
There is a question:
if the event is crossing the current month, it will not be shown.
example:
ID f8a58d0c8280db2340863a1ac7aa60b0
ID_USER e576c22ce89f38ee422ec818807b99b8
BODY_NAME test_event
BODY_START 1422720000
BODY_END 1428422400
IS_DONE 0
TIME_CREATED 1422845255
TIME_UPDATED 0
TIME_DELETED 0
If you do the query, this event will not show.
how can i fix this? Thanks.
If I understand you correctly, simplify your query to be (in pseudo code):
BODY_START <= month_end and BODY_END >= month_start
Your original query query above requires that events must either start or end during the month, but that's not actually what you want - you want any event that exists for any part of the month.
Related
iam developing a webSite using php and mysql and am really beginner in this area.. Now iam stuck in a place where i have to check the current date is newer than the date in my database
so i have written a code like this but it's not working
$SQL = "UPDATE adverts SET active='0' WHERE enddate<NOW()";
$result = mysqli_query($con,$SQL);
in the above code 'advert' is my table name and 'enddate' is where the column containing date in database
can anybody please help me?
As Anthony described in comments, it's a good idea to check if your query is working in PHPMyAdmin at all, by going to PHPMyAdmin -> SQL -> Run Query. This way you can distinguish if it's an MySQL Error or an PHP Error.
UPDATE adverts SET active = 0 WHERE ( enddate < NOW() )
I've set active '0' to 0, simply because I believe it'll be an Integer field - secondly, there's a small but important difference in your enddate:
is it a date field or a datetime field? See below:
SELECT NOW(); // You will get 2010-12-09 17:10:18
SELECT CURDATE(); // You will get 2010-12-09
Source: MySQL Curdate() vs now()
You can use affected_rows() to see if you query did work, but just didn't meet any criteria
$sql = "UPDATE adverts SET active = 0 WHERE ( enddate < NOW() )";
$queried = mysqli_query( $con, $sql );
if ( mysqli_affected_rows( $con ) >= 1 ) {
//We know some rows were effected.
}
Did you tried
UPDATE adverts SET active='0' WHERE enddate<DATE(NOW())
also, is it adverts or advert?
Or you can try with CURDATE()
UPDATE adverts SET active='0' WHERE enddate<CURDATE()
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)
Okay guys, this probably has an easy answer but has been stumping me for a few hours now.
I am using PHP/HTML to generate a table from a MySQL Table. In the MySQL table (TimeRecords) I have a StartTime and EndTime column. In my SELECT statement I am subtracting the EndTime from the StartTime and aliasing that as TotalHours. Here is my query thus far:
$query = "SELECT *,((EndTime - StartTime)/3600) AS TotalPeriodHours
FROM TimeRecords
WHERE Date
BETWEEN '{$CurrentYear}-{$CurrentMonth}-1'
AND '{$CurrentYear}-{$CurrentMonth}-31'
ORDER BY Date
";
I then loop that through an HTML table. So far so good. What I would like to do is to add up all of the TotalHours and put that into a separate DIV. Any ideas on 1) how to write the select statement and 2) where to call that code from the PHP/HTML?
Thanks in advance!
Try this
$query= "
SELECT ((EndTime - StartTime)/3600) AS Hours, otherFields, ...
FROM TimeRecords
WHERE
Date BETWEEN '{$CurrentYear} - {$CurrentMonth} - 1'
AND '{$CurrentYear}-{$CurrentMonth} - 31' ";
$records =mysql_query($query);
$sum= 0;
while($row=mysql_fetch_array($records))
{
echo"$row['otherFields']";
echo"$row['Hours']";
$sum+=$row['Hours'];
}
echo" Total Hours : $sum ";
Just use a single query with a Sum(). You could also manually calculate it if you're already displaying all rows. (If paginating or using LIMIT, you'll need a separate query like below.)
$query = "
SELECT Sum(((EndTime - StartTime)/3600)) AS SumTotalPeriodHours
FROM TimeRecords
WHERE
Date BETWEEN '{$CurrentYear} - {$CurrentMonth} - 1'
AND '{$CurrentYear}-{$CurrentMonth} - 31'
";
You can do this in the same query if you have a unique id using GROUP BY WITH ROLLUP
$query = "
SELECT unique_id,SUM((EndTime - StartTime)/3600) AS TotalPeriodHours
FROM TimeRecords
WHERE Date BETWEEN '{$CurrentYear}-{$CurrentMonth}-1'
AND '{$CurrentYear}-{$CurrentMonth}-31'
GROUP BY unique_id WITH ROLLUP
ORDER BY Date
";
In this instance the last result from your query with contain NULL and the overall total. If you don't have a unique ID you will need to do it in PHP as per Naveen's answer.
A few comments on your code:
Using SELECT * is not considered good practice. SELECT the columns you need.
Not all months have a day 31 so this may produce unexpected results. If you're using PHP5.3+, you can use
$date = new DateTime();
$endDate = $date->format( 'Y-m-t' );
The "t" flag here gets the last day of that month. See PHP docs for more on DateTime.
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
I'm making a website/database solution with database viewing/querying rights for the administrator.
I would like to give the user the option to insert two dates, with the page then producing all orders within the selected period.
I'm aware of how to retrieve variables from a form, it's the back end stuff I'm unsure about and applying the dates to the records.
An example to maybe get you going in the right direction:
$results = mysql_query(
sprintf("
SELECT [your fields]
FROM [your table]
WHERE [date field] > '%s'
AND [date field] < '%s'",
mysql_real_escape_string($enteredStartDate),
mysql_real_escape_string($enteredEndDate)
)
);
Edit:
Dates should be in the format "YYYY-MM-DD HH:MM:SS".
Example fetch / display:
while ($row = mysql_fetch_assoc($results)) {
echo $row['fieldName'];
}
You of course need to be connected to a DB for any of this to work, see PHP Manual mysql_connect
If you are still having problems I think you need to do some reading on using PHP / MySQL, a simple Google search gives a whole stack of possible reading material.
use sql between operator,
where table.date_column_name BETWEEN '$startDate' AND '$endDate'
This query will do -
$query = SELECT * FROM table_name WHERE create_time <= '" . $EndDate . "' and create_time >= '" . $StartDate . "';
where create_time is the the field in your table where you save date and time