SQLite3 Time Problem 11 getting selected before 2 - php

I have a SQLite database that has a time field set as a text type. And it contains values like 11:30 PM, 2:30 PM, etc and I need to select time and date by date & time ASC. I use the following query SELECT * FROM schedule ORDER BY date ASC, time ASC LIMIT 50
But the problem is I get values such as 11:30PM before 2:30PM
How can I avoid this?
Thanks!

the problem is that you are storing in human readable format a value that should be meaningful for a machine. Since sqlite doesn't have a native time type, you'll have to make do with the next nearest approximation, which is a numeric type. You could store both date and time as a single number, for instance as seconds since January 1st, 1970, and then format those values for presentation to users at the last moment.

Related

I want to have a column in MySQL database that automatically calculates difference in DATETIME

In MySQL database, I have the following table that stores work clock in and clock out times. I would like the hours column to auto calculate the DATETIME difference in the database.
ID Clock In Clock Out Hours
1 10:00 17:00 7
2 09:00 16:00 7
3 09:00 15:30 6.5
The SQL statement im using to preview the results is:
SELECT TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference` FROM records
I just want to know how i can apply this to the hours column in the db to auto populate when records are created.
The simplest way to do what you want is to use a view:
CREATE VIEW v_records AS
SELECT v.*, TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference`
FROM records;
This conveniently calculates the value when it is used. That ensures that the most recent data is used for the calculation.
If you want to get fancy, you can write a trigger. However, you need to deal with both updates and inserts.

how to calculate 08:30:00 and login [ this field type is date and time ] difference?

I am calculating a difference between check-in time (08:30:00) it's fixed and login time will be fetched from the database. I find the difference between both of them. for eg. The person login 11:40:00 then the output will be shown as you are "3hrs 40min 00sec" late today.
I have tried this code.
in this query, i am converting login field (date and time into only time ).
select DATE_FORMAT(login,'%H:%i:%s')as TIMEONLY
from attendance
where eid='".$eid."'
ORDER BY login DESC limit 1
please give me solution of a time difference.
Use strtotime(string $time) method.
strtotime — Parse any English textual datetime description into a Unix timestamp
Now you can get the difference between two timestamps and then apply normal mathematical operations to convert them into hours, minutes and seconds.

Mysql query or code for time range search

Is there anybody who write query for time range. I am searching from last 6 days when I get the result it will all about dates. Where are times?
I need a solution for searching e.g. from 12:00:00 PM to 04:00:00 PM in datetime field. So how can I search that from mysql table on whatever dates the have. But the result will how on that time range. Any body have solution for that.
Use the TIME() function, it extract the time portion from a datetime field https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
SELECT TIME(datetime_col) FROM table
WHERE TIME(datetime_col) BETWEEN '12:00:00' AND '16:00:00'

PHP/SQL : Sorting

I have field(time) with data like this:
9:00-11:00 am
7:00-9:00 am
6:30-7:30 pm
1:00-2:30 pm
Select * from table order by time ASC
result:
1:00-2:30 pm
6:30-7:30 pm
7:00-9:00 am
9:00-11:00 am
I cant sort it correctly. Any help will do.
Thanks
Store your times in two separate TIME fields (start_time and end_time for example), which can reasonably be sorted by the database. Just storing a time range as text doesn't tell the database anything, it can't magically understand what it means.
Store your date ranges in two TIMESTAMP columns in MySQL, then you can sort on the columns however you want. You can even do more advanced sorts like sorting on the duration between the two timestamps easily using MySQL.
I am not recommending that you do this, but to show you why you'd want to split this up into multiple columns as #deceze recommended.
You can use mysql's STR_TO_DATE() to (kindof) convert the string to a date. Note that it completely ignores the first part of the time range, so it's effectively sorting on:
11:00 am
9:00 am
7:30 pm
2:30 pm
The query is:
SELECT
time,
STR_TO_DATE(time,'%h:%i-%h:%i %p')
FROM time_table
ORDER BY STR_TO_DATE(time,'%h:%i-%h:%i %p') ASC
A second query that effectively sorts on this:
9:00am
7:00am
6:30pm
1:00pm
is:
SELECT
time,
STR_TO_DATE(CONCAT(SUBSTRING_INDEX(time,'-',1), SUBSTRING(time,LOCATE(' ', time)+1)), '%h:%i%p')
FROM time_table
ORDER BY STR_TO_DATE(CONCAT(SUBSTRING_INDEX(time,'-',1), SUBSTRING(time,LOCATE(' ', time)+1)), '%h:%i%p') ASC
(I'm certain someone that's more proficient with regular expressions would be able to shorten this one).

Help with a MySQL Unix timestamp Query

I am trying to create a custom MySQL for use with the Expression Engine CMS. The purpose of the query is to display events that are happening today or in the future.
The problem is that the EE field type that allows you to put in the date and converts it into a unix timestamp. If I pick the 26th July it puts in the date value "25th July 23:00".
As you see from my query below it almost works but I need to add 24 hours onto the values that are used in the conditional part of the statement. I want events that occur on the day "for example today 25th July" to be displayed up until 23:00 hours that day then be removed.
I almost have it I am just stuck on how to add 24 hours to the conditional.
SELECT t.entry_id,
t.title,
t.url_title,
d.field_id_13 AS event_lineup,
d.field_id_14 AS event_details,
d.field_id_15 AS event_day,
d.field_id_16 AS event_flyer_front,
d.field_id_17 AS event_flyer_back,
d.field_id_18 AS event_facebook,
d.field_id_12 AS event_date
FROM `exp_weblog_titles` AS t
NATURAL JOIN `exp_weblog_data` AS d
WHERE d.weblog_id = 5
AND CAST(d.field_id_12 AS UNSIGNED) >= (unix_timestamp(Now()))
ORDER BY d.field_id_12 ASC
What I think might be happening is your timestamps get adjusted for the time zone, and that adjustment is configured differently in the CMS and on the server.

Categories