Mysql query or code for time range search - php

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'

Related

Laravel Get rows by time for all dates

I'm trying to get "Usage Patterns"
So I need to extract from my db all the rows from the last 30 days but only the ones that created at
7:00 - 14:00 for example
the values is from "created_at" so its datetime
I thought just to extract all and check str_contais for 14:00 or something
But I'm sure its not the best idea
You should be able to combine DATE and TIME queries to get what you need.
->whereBetween(DB::raw('DATE(created_at)'), [$from_date, $to_date])
->whereBetween(DB::raw('TIME(created_at)'), [$from_time, $to_time])
Example SQL
SELECT * FROM table WHERE
DATE(created_at) BETWEEN '2019-09-22' AND '2019-10-22' AND
TIME(created_at) BETWEEN '07:00:00' AND '14:00:00'

Select date stored as Unix Timestamp and compare with given Unix TimeStamp

I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);

mysql query, how to go about it?

I am storing data in a mysql database with unix time()
I want to select data out for days, and echo it out like this:
Monday - value
Tuesday - value
Wednesday - value
Thursday - value
Friday - value
Saturday - value
Sunday - value
value is saved in the database as value
the times are saved in the database as time
So basically I want to select time from the database in a query that groups all of them as a day, Monday.. do you get what I mean?
Any help with this would be greatly appreciated.
First, replace that integer with a real DATETIME column, then use MySQL's built-in functions to easily GROUP BY the DAYOFWEEK(column)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
There is no advantage to storing an integer instead of a real date. Not only can you easily convert to a UNIX timestamp if you need that in your application, but you'd save storage space too.
To convert to MYSQL native date, use FROM_UNIXTIME(). DAYNAME will return the day of the week.
SELECT DAYNAME(FROM_UNIXTIME(col))
For example:
SELECT DAYNAME(FROM_UNIXTIME(1196440219))
Further, to group by you could use something like this:
SELECT DAYNAME(FROM_UNIXTIME(col)), ... FROM table GROUP BY DAYNAME(FROM_UNIXTIME(col))
Maybe hits post can help you: MySQL Query GROUP BY day / month / year
Regards!

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.

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories