php mysql storing just hours minutes seconds - mysql

I know a typical timestamp in any format readable or otherwise is always equivalent to a date time second day month year etc. However I want to be able to search by hours minutes seconds where the day month year are irrelevant. So I am trying to wrap my head around that ability and what would be the best method of storing time so I can create searches around that alone without m-d-y getting in my way.

Try using the TIME field type. The TIMESTAMP field type should only be used anyway when you want MySQL to update the field when updating the row.

$hour = date("H",$date); $minute = date("i",$date); $second = date("s",$date);
and save them on your table as hour,minute and second

Related

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

Make a data field in MySQL database update automatically each day or week?

I have a field in a MySQL database which i want to add a certain number each day or week whichever is easier to achieve. For example i have a field with the number 2000 in it and everyday or week i want to add 200 to that number.
Is this possible and if so how can i achieve it?
Just write a SQL function which will return the field times number of week for example.
I do not post a complete function because the question is not that clear about what you expected to return

ActiveRecord finding only hourly records

I've got a table with a datetime column that has records for every minute (logging data every minute)
I want to get only the records that occurred at the top of the hour(1pm, 2pm, 3pm, 4pm, etc).
What would be the best way to get this?
You could use extract to find the values which have zero for the minutes:
top_of_the_hours = Model.where('extract(minute from your_datetime) = 0')
That will also work with PostgreSQL but you'd need to switch to strftime for SQLite.

Checking if the UNIX timestamp stored in MySQL DB is this weeks or this months?

I have a MySQL DB, and in one of the tables I have stored the time in which the content was submitted its in the form of a UNIX timestamp, the column is called content_time. Below are two pseudo examples of queries to demonstrate what I'm trying to accomplish, just not sure how to go by doing this (although I understand I will need do some some comparisons between the current and stored timestamps within the WHERE clause):
SELECT * FROM content
WHERE content_time = THIS WEEKS
(the content was posted at any time/day within the current week)
SELECT * FROM content
WHERE content_time = THIS MONTHS
(the month and year from content_time match with the current)
Appreciate all help.
See MySQL's Date and Time Functions, specifically FROM_UNIXTIME(), WEEK() and MONTH(). Keep in mind that when checking is it the same week or month you probably also want to check is it the same year.
Another option is to generate start and end timestamps for the time range youre intrested in (ie timestamp for the beginning of the week and for the end of the week) and then use WHERE(content_time BETWEEN start AND end)

Increment a value for entire database

So we have a database. Every so often we'll add a new data set and the oldest dataset should be removed.
Say the DB is Early June: 18 records, Late June: 15 records, Aug: 23 records. When we add the next records we want to remove the Early June records. However adding new data isn't totally regular.
My second thought is to have an "oldness" field for each record and before adding a new data set, increment all of the "oldness" fields by 1, then removing all the data that has "oldness" of 3.
I'm worried that this is not the optimal way to do this, but I'm not sure it matters since it's a fairly small database that is updated infrequently. That said, I'm certainly open to suggestions for a better algorithm to accomplish this.
I'm assuming your data is stored such that it has a timestamp (date) column for each report, and that you always want to remove data that is x (in this case, 3) months old. If so, you might think about using mysql's DATEDIFF function.
For example, it might look something like this:
DELETE from table1 WHERE datediff(CURRENT_DATE(), datecol) > 89
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Is it always by month?
UPDATE table SET status = "oldness" WHERE date_inserted <= NOW() - INTERVAL 3 MONTH
With a little help from user937146 I think I've got the solution. Basically
$query = "SELECT MIN(timestamp) FROM new_items WHERE type = 'Book' ";
Then it's just a matter of taking that timestamp, adding an hour (since the data can take more than a second to upload, probably one minute would be sufficient) and then deleting everything from the database older than that timestamp.