mysql select date using timestamp - php

in my database i use a column named startdate and in the column there are rows with timestamps, looking like: 1410178260
Normally, when i use a datetime field and i want to select all the items with the date of today, i run this query:
$sql = "SELECT id FROM agenda2 WHERE DATE(startdate) >= CURRENT_DATE()";
But now, using the timestamps, i don't know how to make a query that selects all the items inserted today.
Can someone help me with that?

You need to convert to date using the function from_unixtime()
mysql> select FROM_UNIXTIME('1410178260');
+-----------------------------+
| FROM_UNIXTIME('1410178260') |
+-----------------------------+
| 2014-09-08 17:41:00 |
+-----------------------------+
So you may do as
SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()

If you're using unix timestamps then you've got to use
$sql = "SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()";
Consider that both versions can't make use of an index.
see FROM_UNIXTIME

Related

Order mysql query by date and time which are separate columns

I wish to order my mySQL query by date and time as if they were a combine column (datetime) however they are a separate column (Date and Time). I need to be able to display all the records from a table that are after the current time (using Date("")).
If we assume you have table t with structure:
id | date | time
Then to order by date and time you do:
SELECT * FROM t ORDER BY date,time
To select everything that's newer than the current time do:
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME())
There's an alternative way to do the second one which is based on Creating DATETIME from DATE and TIME so you can refer to that if you need to.
You can combine the above and do a :
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME()) ORDER BY date,time

mysql select ID where date is current date

i have this table structure:
Rows
ID | Counter | Dates
Values
1 | 100;300;44 | 01.01.2016;02.11.2016;03.03.2017
each ID is connected to an separated user.
at the moment i have 100 users, so 100 data in my table.
now i need an sql command, which show me the the ID from the user, which have a date in the Dates-Row, which is the same like the current date
This is untested, but it should be:
SELECT * FROM `table` WHERE Dates LIKE CONCAT('%',DATE_FORMAT(CURDATE(),'%m.%d.%Y'),'%');
This will select the current date CURDATE(), put it in the format you use in your table, then stick wildcards on each end so it will look inside of the string within your Dates column.

trouble in short date when field type is varchar

i have date field in database but unfortunately date field type is varchar. Date inserts like 08-07-2010(dd-mm-yy). i want to short data by date asc order (year) like 08-07-2009, 07-07-2010. when i am using "order by date asc" is short data by year like 07-07-2010, 08-07-2009 . Please give me any suggestion if you have my query is
select * from tbl_name order by date asc
Thats the issue , nerver store date in varchar, you should store dates in mysql native date data types.
However in your case you need to convert the varchar date to real date using the function
str_to_date
mysql> select str_to_date('08-07-2010','%d-%m-%Y');
+--------------------------------------+
| str_to_date('08-07-2010','%d-%m-%Y') |
+--------------------------------------+
| 2010-07-08 |
+--------------------------------------+
So the query should be
select * from tbl_name order by str_to_date(date,'%d-%m-%Y') asc

Using a datetime margin in a SQL Query

Bit of a double barrel question here. I have a table with 4 columns:
id | userid | systemid | datetime
Every time someone logs in a record is added to this table. What I'm trying to achieve is a query that allows me to count the number of UNIQUE userid's that are logged where system id = x from between 1st of the current month until the current time.
I'm hovering with the current syntax but am not aware of the best way to only add to the count once for each userid:
$mysqli->query("select count(userid) as c from log_logins where datetime between '[1st of this month]' and curdate()");
$result = $mysqli->fetch_object()->c;
Also, for this to work does the datetime column have to be a unix timestamp? The datetime column is currently set as this value: <?php date('d/m/Y H:i', time()); ?>
Thanks for any help in advance!
Michael
Use COUNT(DISTINCT userid):
$mysqli->query('
SELECT COUNT(DISTINCT userid) AS c
FROM log_logins
WHERE systemid = x
AND datetime BETWEEN \'[1st of this month]\' AND CURDATE()
');
As regards the format of the datetime column, it should be a MySQL TIMESTAMP type (which is stored internally as a UNIX timestamp).

SELECT difference between two dates and sum

I have this table in mysql
Date one | Date tow |
2012-05-20 | 2012-05-04 | = 16 days
2012-05-12 | 2012-05-08 | = 4 days
= 20 days
and i want to select difference between two dates and then sum all days.
If you should do it in MySQL, you could use DATEDIFF function.
SELECT DATEDIFF(dateone, datetwo) AS d FROM tablename
and then you could aggregate this result the way you want, example
SELECT SUM(DATEDIFF(dateone, datetwo)) AS s FROM tablename
You can do it also in PHP after fetching the dates
You can try the below query. hope it will sort out the issue.
SELECT SUM(DATEDIFF(DATE1,DATE2)), FROM TABLE
You can use the DATEDIFF function in mysql.
SET #runtot:=0;
SELECT DATEDIFF(one,tow) AS diff, (#runtot := #runtot + DATEDIFF(one,tow)) AS runningsum
FROM table
SELECT SUM(DATEDIFF(`Date one`, `Date tow`)) FROM `Table`
See DATEDIFF() and SUM().
Note that in MySQL, you do not need to use GROUP BY in order to be able to use the aggregate function SUM(). In that case, MySQL will regard all rows as a single group.

Categories