I'm getting a weird return when executing this query :
SELECT * FROM rrp
WHERE end > "2012-12-31"
nothing is returned, although I have one row on this table which "end" column is greater than "2012-12-31":
rrp
id_r | id__b | start | end | quantity
27 29 2012-01-01 2012-05-05 1
31 29 2012-11-01 2013-01-01 1
EDIT : startand endare date fields
EDIT : I used wrong database for my tests => wrong result
the issue was coming from Zend_Date when adding a day to a date:
$start = "2012-12-31";
$nStart = new Zend_Date($start, "YYYY-MM-dd");
$end = new Zend_Date($nStart);
$end->addDay(1);
When i echoed $end : echo $end->get("YYYY-MM-dd");
it outputs 2013-12-31
Most likely an issue with how the dates are formatted
This should help
http://dev.mysql.com/doc/refman/5.0/en/using-date.html
If end is a DATE column, it should work as expected:
SELECT
STR_TO_DATE('2013-01-01', '%Y-%m-%d') < "2012-12-31",
STR_TO_DATE('2012-05-05', '%Y-%m-%d') < "2012-12-31"
... returns 0, 1 in my box.
The only possible flaw I can think of is that your system's default date format is not %Y-%m-%d:
SELECT ##DATE_FORMAT
In that case, you need to specify a format every time:
SELECT *
FROM rrp
WHERE end > STR_TO_DATE('2012-12-31', '%Y-%m-%d')
Related
I am selecting start and end date of a project from project_stage named table.
Here is the table elements
stage_start_date stage_end_date planned_start_date planned_end_date
2019-01-28 10:12:01 2020-12-08 09:05:28 2019-01-12 01:01:00 2020-12-01 00:00:00
Here datatype is DATETIME
Here is the code
SELECT ps.stage_start_date AS actual_start_date,
ps.stage_end_date AS actual_end_date,
ps.stage_planned_start_date AS planned_start_date,
ps.stage_planned_end_date AS planned_end_date
FROM project_stage AS ps
JOIN map_project_user AS mpu ON mpu.user_id = 22 AND mpu.project_id = 1 AND mpu.tenant_id = ps.tenant_id AND ps.project_id = mpu.project_id;
Result on JSON response
{
"actual_start_date": "2019-01-28T04:42:01.000Z",
"actual_end_date": "2020-12-08T03:35:28.000Z",
"planned_start_date": "2019-01-11T19:31:00.000Z",
"planned_end_date": "2020-11-30T18:30:00.000Z"
}
Here date time is changing its not the actual datetime which is in the table,why the date is changing on result.Here is the expected output
Expected Result
{
"actual_start_date": "2019-01-28 10:12:01",
"actual_end_date": "2020-12-08 09:05:28",
"planned_start_date": "2019-01-12 01:01:00",
"planned_end_date": "2020-12-01 00:00:00"
}
MYSQL DATATYPE is DATETIME. Data base timezone is in UTC and System timezone is also showing UTC, How can I covert this datetime corresponding to timezone of users system
According to the data examples, the Timezone issue appears to be in the code that converts the SQL result to JSON. Since the time difference between the database and the JSON is -05:30, it seems that the "JSON transformer" assumes that the result of the SQL query is IST (UTC +05: 30) and converts the time to UTC (subtracts 5:30).
The correct fix should be done in the "JSON transformer". However, if the requirement is to achieve the "corrected date" by modifying the SQL query, you can use the CONVERT_TZ (dt, from_tz, to_tz) function. This adds +5:30 and "JSON transformer" subtracts 5:30 later resulting the time being unchanged.
Something like that:
SELECT DATE_FORMAT(CONVERT_TZ(ps.stage_start_date, '+00:00', '+05:30'), "%Y-%m-%d %H:%i:%s") AS actual_start_date,
DATE_FORMAT(CONVERT_TZ(ps.stage_end_date, '+00:00', '+05:30'), "%Y-%m-%d %H:%i:%s") AS actual_end_date,
DATE_FORMAT(CONVERT_TZ(ps.stage_planned_start_date, '+00:00', '+05:30'), "%Y-%m-%d %H:%i:%s") AS planned_start_date,
DATE_FORMAT(CONVERT_TZ(ps.stage_planned_end_date, '+00:00', '+05:30'), "%Y-%m-%d %H:%i:%s") AS planned_end_date
FROM project_stage AS ps
JOIN map_project_user AS mpu ON mpu.user_id = 22
AND mpu.project_id = 1
AND mpu.tenant_id = ps.tenant_id
AND ps.project_id = mpu.project_id;
Edit: Another option: simply add +5:30 to the dates:
SELECT ps.stage_start_date + interval 5 hour + 30 minute AS actual_start_date,
ps.stage_end_date + interval 5 hour + 30 minute AS actual_end_date,
ps.stage_planned_start_date + interval 5 hour + 30 minute AS planned_start_date,
ps.stage_planned_end_date + interval 5 hour + 30 minute AS planned_end_date
FROM project_stage AS ps
JOIN map_project_user AS mpu ON mpu.user_id = 22
AND mpu.project_id = 1
AND mpu.tenant_id = ps.tenant_id
AND ps.project_id = mpu.project_id;
Your backend changes the time format. Check you API which connects to the database.
I am using PHP with MySQL and would like to select rows that have a booking time within 2 hours from now. How do I compare what is in my database with the NOW() MySQL function?
I have columns pickupDate in the format yyyy-mm-dd and pickupTime in the format HH:mm (24-hour). I have tried creating a query with NOW() which returns the a 12-hour time as HH:mm:ss e.g. 2019-05-24 07:54:06 . I can't figure out how to format this to 19:54, or if I should use a different function instead.
For example, if the current date and time is 24/05/19 19:54:06, I would like to select rows between 19:54 and 21:54 on this date.
My table structure is:
referenceNo VARCHAR(100)
pickupDate DATE
pickupTime VARCHAR(100)
You need to create a DATETIME compatible value out of your pickupDate and pickupTime (which you can do by CONCATing them together), then you can compare that with a time range from NOW() to 2 hours later:
SELECT *
FROM yourtable
WHERE CONCAT(pickupDate, ' ', pickupTime) BETWEEN NOW() AND NOW() + INTERVAL 2 HOUR
Demo on dbfiddle
To add two hours in php
$hoursnow = date('H:i');
$timestamp = strtotime(date('H:i')) + 60*60*2;
$plusTwohours = date('H:i', $timestamp);
And $PlusTwohours using this variable frame the query like below
Sql Query:
$sqlQuery = 'select * from foodorder where pickupDate=DATE(NOW()) AND pickupTime>='.$hoursnow.' and pickupTime<='.$plusTwohours;
$result = mysql_query($sqlQuery);
variable $result will have the values of query
For Second Scenario: Adding hours to end of the day May 24 23:30:00
This should be handle by two different date for same column pickupDate
$d = new DateTime('2011-01-01 23:30:30');
$startDate = $d->format('Y-m-d H:i:s'); // For testing purpose assigned manually
$starttime = date('H:i');
// Here Process start, storing end date by adding two hours
$enddate1 = strtotime($startDate) + 60*60*2;
$enddate = date('Y-m-d', $enddate1); // Extracting date alone
$endtime = date('H:i', $enddate1); // Extracting time alone
Have to compare start and end date for column pickupDate, here is the query
$sqlQuery = "select * from foodorder where pickupDate>=DATE(".$startDate.") AND pickupDate<=DATE(".$enddate.") AND pickupTime>='".$starttime."' AND pickupTime<='".$endtime."'";
$result = mysql_query($sqlQuery);
Is there a way to write a MySQL query to get records by monthly for example:
Starting from the current month:
----- Jan -----
Record 1
Record 2
----- Feb -----
Record 1
Record 2
----- March-----
Record 1
Record 2
I have starting and ending timestamp and start_date, end_date(MM/DD/YYYY format) column in my db table.
solved by getting start date & end date of month
$month_start_time = strtotime(date('Y-m-01', $timestamp)." 00:00:00");
$month_end_time = strtotime(date('Y-m-t', $timestamp)." 23:59:59");
$sdate = date('d', $month_start_time);
$edate = date('d', $month_end_time);
$j=0;
for($k=$sdate; $k <= $edate; $k++){
$start_date = $k.date('/m/Y', $month_start_time);
$stimestamp = strtotime(date('Y/m', $month_start_time).'/'.$k." 00:00:00");
$end_date = strtotime(date('Y/m', $month_start_time).'/'.$k." 23:59:59");
//then pass in mysql query
}
I have a table of events, each of which has a start date and an end date.
I need to display a list of days and show which events are occurring on each day.
E.g. Say I have on event that runs mon-wed and another that runs tues-thurs I'm trying to create a table like so:
Mon: Event 1
Tue: Event 1, Event 2
Wed: Event 1, Event 2
Thu: Event 2
The only way I can see to do this is either by running a query for each day or by loading all events for given date range and then duplicating them in my code if they last for more than one day. Both approaches seem hacky and I'm sure I'm missing something.
Is there a neater way to do this?
Structure:
id INT
name VARCHAR
start_date DATE
end_date DATE
Current query (slightly simplified because this database structure is appalling):
SELECT *
FROM events
WHERE start_date <= $somedate
AND end_date >= $somedate
...where $somedate is a given date. (Yes, I am escaping it properly in my code!)
Select your events for a particular date range (I assume a particular month). Then loop over the days in that month and have another, inner loop that displays any events that occur on that date.
<?php
$events = getEvents(); // some function that returns events from database
echo '<ol class="calendar">';
for ($i = 1; $i <= cal_days_in_month(); $i++) {
echo '<li>';
$beginning = mktime(0, 0, 0, date('n'), $i, date('Y'));
$end = mktime(23, 59, 59, date('n'), $i, date('Y'));
$events_on_day = array();
foreach ($events as $event) {
if ($event->start_date <= $end && $event->end_date >= $beginning) {
$events_on_day[] = $event;
}
}
if ($events_on_day > 0) {
echo '<ul>';
foreach ($events_on_day as $event_on_day) {
echo '<li>' . $event_on_day->name . '</li>';
}
echo '</ul>';
}
}
echo '</ol>';
?>
There's probably a better way, but off the top of my head I would say to create a table of possible dates, then simply
select * from events
inner join possibleDates on possibleDates.Date >= start_date and possibleDates.Date <= end_date
First you'll propably have to get all dates between start_date and end_date of each event.
Put the in a temp. table like:
|Weekday| Event_name|
|Mo | Event1 |
|Tu | Event1 |
|We | Event1 |
|We | Event2 |
|Th | Event2 |
|Fr | Event2 |
and then group_concat the events by group by on weekday.
This link will help you to get the dates between start and end date of each event.:
Get a list of dates between two dates
You can do it in one query if you must, though I would advise getting a weekday-table like Jan Zeiseweis also suggests. Working SQLfiddle: http://www.sqlfiddle.com/#!2/e618d9/1/0
SELECT wk.day, GROUP_CONCAT(e.name, '') events
FROM (
SELECT 'Monday' day, 0 dayIndex
UNION
SELECT 'Tuesday' day, 1 dayIndex
UNION
SELECT 'Wednesday' day, 2 dayIndex
UNION
SELECT 'Thursday' day, 3 dayIndex
UNION
SELECT 'Friday' day, 4 dayIndex
UNION
SELECT 'Saturday' day, 5 dayIndex
UNION
SELECT 'Sunday' day, 6 dayIndex
) wk
LEFT JOIN Events e
ON WEEKDAY(e.start) <= wk.dayIndex
AND WEEKDAY(e.end) >= wk.dayIndex
GROUP BY wk.day ORDER BY wk.dayIndex
Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?