EPOCH Unix MYSQL Query - php

I'am using a script that saves some fields into my database as Epoch & Unix Timestamp format, (I think because the field is submitting as something like 1515469971), the column is an "int" format type.
I want to know, how I can make a query, to search all the inputs that have created on the current month.
I am so confused about it, hope you can help me :)

Firstly, show me your table structure with show create table table_name.
If you're using an int to store the time. There are two ways to do that:
Convert the field to a date string with from_unixtime. Then you got a string like '2018-01-08 05:05:05', and just compare the month value.
Calculate the very beginning and the end of the current month in advance. Then just select * from table_name where time >= beginning_of_the_month and time <= end_of_the_month.

Related

How to select data by using range of dates in MySql query?

I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal

Php/MySQL Auto date checking/comparing

Wanted to ask You how can I setup something on my php website, that would everyday automatically check and compare current date to all the database datetime entries and delete the rows of the dates that are in the past (for ex. if the current date is 2014-03-17, it would delete the rows that have datetime of 2014-03-16 ).
Because I basically have a TV-package website (not a real thing, just for a class), where you can order a package, you enter for how long and it adds that amount to current date, writes the order into database with the date written into a field named "expires". Would it make sense if I just wrote the checking function into the index, so when someone visits the site it would delete it? If so, how could I compare the two dates?
The DB example looks something like this: http://s29.postimg.org/7sbgj2hnr/dbtest.png
Although I highly recommend a scheduled task, you can do it in PHP by calling:
$sql = "DELETE FROM tableName WHERE `expires`<'".date('Y-m-d')."'";
Convert the date to a unix timestamp and compare it against the value of time() like you would any other integers.

PHP MySql: how save timestamp UTC, like "Flickr"?

For default i'm using this snippet in my codes
setlocale(LC_TIME,'it_IT');
and I did save all my dates in mysql in a timestamp format.
In a view of make international one of my codes, i would like to save in MySQL a similar value
2013-07-12T07:59:27+0000
but of course with the +2 hours. So a Polish user will have a +3 and so on...
What's the best field for mysql to store that value? I need also to work with date, from PHP and/or from directly MySQL (for example)
SELECT id WHERE data BEETWEN [...]
Of course if I start with correct way, i don't need to change in future all my dbs, codes, etc...
Thank you very much!
For me is better store date time as UNIXTIME, it avoid the time difference between user from different locations.
You can use:
SELECT * FROM table_name
WHERE field_name
BETWEEN UNIX_TIMESTAMP('2013-07-12 07:59:27')
AND UNIX_TIMESTAMP('2013-07-13 07:59:27')
to get the Date Range

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Question regarding MySQL timestamp comparisons

I'll explain my goal first: I want the user to query the database, and return rows only if those rows have been updated since their last query. No sense returning data they'd already have. So I created a column called 'lastupdated', a timestamp type which autoupdates every time any content in the row is updated. This works fine. Now, I want to form the query correctly. The user will have their previous query's timestamp saved, and via php will use it to compare their previous query's time with the time each row has been updated. If the row was updated after their last query, the row should be returned.
I made something like this,
SELECT * FROM users WHERE '2011-02-26 01:50:30' <= lastupdated
but its obviously much too simple. I checked the MySQL manual and found this page MySQL Time/Date Page. I'm sure the answer is here, but I've read through it any nothing really makes sense. I have a timestamp in the same format used by the MySQL timestamp type, but I don't know how I will compare them. Thank you very much for your help.
That query is exactly how you'd do it. As long as a stringified date-time is in MySQL's preferred format (yyyy-mm-dd hh:mm:ss), then it will be internally converted into a datetime value, and the comparisons will go ahead.
You'd only need the date/time functions you found if you want to do something more complicated than simple "greater/less than/equal" type comparison, e.g. "any records that have a December timestamp".
As Marc said, your code should work. But you probably want to do this programmatically with a variable for the time instead of the literal.
If you don't have the date-time specified as a string, but rather as a timestamp (e.g. from using the php time() function), then you can use the following query:
$query = "SELECT * FROM users WHERE FROM_UNIXTIME(" . $timestamp . ") <= lastupdated";
The key is the FROM_UNIXTIME() MySQL function.

Categories