MysQL Get Time via Query - php

The time in row 'Signup' is stored like this: "1427538785"
So I am not able to use this method: WHERE DATE(Signup) = DATE(NOW())
But need to get the query where time of signup is today..
Any Idea how to solve this?

You need from_unixtime
mysql> select from_unixtime('1427538785','%Y-%m-%d');
+----------------------------------------+
| from_unixtime('1427538785','%Y-%m-%d') |
+----------------------------------------+
| 2015-03-28 |
+----------------------------------------+
1 row in set (0.00 sec)
So the query will be
WHERE from_unixtime(Signup,'%Y-%m-%d') = curdate();

WHERE DATE(FROM_UNIXTIME(Signup)) = curdate()

Related

Multi select on same database with different conditions

i have pagination and i should call database for two times!
it's meant i should call :
SELECT COUNT(users_id) FROM users
for get all of database rowCount
and in another database fetch i have :
SELECT * FROM users ORDER BY users_id ASC LIMIT 1, 2
for create pagination
is there anyway to create one database call and get the same results ?
i try this:
SELECT COUNT(users_id),* FROM users ORDER BY users_id ASC LIMIT 1, 2
but this, call just 2 (count users_id) and broke pagination :(
Thanks and sorry for my poor english.
You can get this with 2 Queries:
1) first get your result with LIMIT and OPTION SQL_CALC_FOUND_ROWS
2) get the num rows without LIMIT
SELECT SQL_CALC_FOUND_ROWS t.* FROM yourTable 1 LIMIT 2;
SELECT FOUND_ROWS();
sample
mysql> SELECT SQL_CALC_FOUND_ROWS t.* FROM yourTable t LIMIT 2;
+-----+------------+
| id | toDate |
+-----+------------+
| 111 | 2017-03-02 |
| 111 | 2017-03-20 |
+-----+------------+
2 rows in set (0,00 sec)
mysql> SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 4 |
+--------------+
1 row in set (0,00 sec)
mysql>
No, it's not possible because it returns different result sets - first query returns single-column result with an int, the other query returns all columns from users.
You need to do two separate queries for this.

TimeZone not set correctly

I have a problem, I'm using mysql with PHP and I have in my database timestamp columns that are automatically set on update by the current timestamp. But when I'm trying to insert rows into the table the rows are added with timestamp different from my current time (before two hours) I have the EST timezone.
I've checked the timezone for the database and it is System and when I searched for that I read that it depeneds on the machine that is connecting, but my machine has EST time, so can anyone explain why I'm having this? and what can I do to set the current timestamp for my database to EST?
I've personally found it to be a lot more reliable to set the timezone in PHP, then grab the timestamp there and pass this to your MySQL query.
Something like:
date_default_timezone_set('America/New_York');
$curtimestamp = date('Y-m-d G:i:s', time());
Then use the value of $curtimestamp in your queries.
The documentation may have been unclear.
time_zone=SYSTEM means that the MySQL Server time zone is derived from the host that the MySQL Server is running on, not the client system.
So, if the time zone on the MySQL server host is different from the time zone of the client host, that explains the behavior that you observe.
Each client (database session) has its own time_zone setting. When the connection is established, the setting is derived from the "default" time zone for the MySQL server.
The setting of this variable determines how DATETIME and TIMESTAMP values are interpreted, when values are sent to the database from the client, AND when existing values are pulled from the database.
A client can issue a SET time_zone statement to change the time_zone for the database session, e.g.
SET time_zone=CST6CDT
(Note that to use named values for time zones, the time_zone% tables in the mysql database must be populated.)
To set the "default" time_zone that will be used for new database connections, you can set the "global" time_zone variable:
SET GLOBAL time_zone=
(This change requires SUPER privilege, and it impacts ALL new database connections to the entire instance. Normally, this value is set in the MySQL server configuration file.)
For TIMESTAMP columns, MySQL stores those as UTC. The values returned to the client depend on the setting of the client time_zone. As a simple demonstration:
mysql> CREATE TABLE mytest
-> ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
-> , ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.11 sec)
mysql> SHOW VARIABLES LIKE 'time_zone';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| time_zone | +00:00 |
+---------------+--------+
1 row in set (0.00 sec)
mysql> INSERT INTO mytest (id) VALUES (NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM mytest;
+----+---------------------+
| id | ts |
+----+---------------------+
| 1 | 2015-01-09 15:41:25 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> SET time_zone = 'CST6CDT';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM mytest;
+----+---------------------+
| id | ts |
+----+---------------------+
| 1 | 2015-01-09 09:41:25 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO mytest (id) VALUES (NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM mytest;
+----+---------------------+
| id | ts |
+----+---------------------+
| 1 | 2015-01-09 09:41:25 |
| 2 | 2015-01-09 09:41:25 |
+----+---------------------+
2 rows in set (0.00 sec)
mysql> SET time_zone = 'EST5EDT';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM mytest;
+----+---------------------+
| id | ts |
+----+---------------------+
| 1 | 2015-01-09 10:41:25 |
| 2 | 2015-01-09 10:41:25 |
+----+---------------------+
2 rows in set (0.00 sec)

Splitting a timestamp record

I got a timestamp record in my msql db which has date and time.
I would to get just the time from it, or just time hour and minute.
How can I split that record?
EDIT here is a picture of my table:
Use MySQL's TIME() function:
SELECT TIME(my_column) FROM my_table
To obtain only the date part, you can use the DATE() function.
Also can try this:
SELECT DATE_FORMAT(now(),'%r') AS timestamp;
| TIMESTAMP |
---------------
| 03:51:43 PM |
SELECT DATE_FORMAT(your_Datetimecolumn,'%r') AS timestamp;
You can also use TIME_FORMAT or DATE_FORMAT to get a specific format e.g. only hh:mm
SELECT TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' );
+-------------------------------------------+
| TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' ) |
+-------------------------------------------+
| 17:25 |
+-------------------------------------------+
1 row in set (0.00 sec)
EDIT
The function of course works for columns of a table as well
SELECT TIME_FORMAT( myColumn, '%H:%i' ) FROM myTable;

mySQL TIME function and Timezone

I want to use the NOW() function with a specific timezone, say GMT+8 for a user and GMT-2 for another user. How can I achieve this?
I am guessing that the time for NOW() is related somewhat to the timezone and time of the SQL server, but I want it to be such that FN(GMT+8) always give me the NOW() in GMT+8 irregardless of the timezone the SQL server is in.
Mini question: How do i display/know the current time of the SQL server?
The NOW() function provides the current time in the local timezone of the server. If you wish to convert to a different timezone, you can use CONVERT_TZ()
UPDATE:
You can use a per-connection timezone (that doesn't affect the system timezone) and get the effect you want:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2011-06-03 22:40:51 |
+---------------------+
1 row in set (0.00 sec)
mysql> SET time_zone = '+08:00';
Query OK, 0 rows affected (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2011-06-04 10:41:15 |
+---------------------+
1 row in set (0.00 sec)

php date() vs mysql MONTH()

i have this unix timestamp value: 1275364800 (1st june 2010).
When printing that value with php:
echo date('m',1275364800)
it returns 6 (thats ok)
But when i select a field from a database: select MONTH(FROM_UNIXTIME(1275364800)) AS month it returns 5
Why?
BTW, if i run this query select FROM_UNIXTIME(1275364800) AS q i get 2010-05-31 23:00:00
To set locales in MySql do the following.
First check what your local is:
mysql> SELECT ##lc_time_names;
+-----------------+
| ##lc_time_names |
+-----------------+
| en_US |
+-----------------+
To make sure its using the correct, If this is corrent the change your apache / php locale.
To change you locale in MySql
mysql> SET lc_time_names = 'en_UK';
Query OK, 0 rows affected (0.00 sec)
en_UK being what you wish to change it to!
mysql> SELECT ##lc_time_names;
+-----------------+
| ##lc_time_names |
+-----------------+
| en_UK |
+-----------------+
Hope this helps you!.
Because 1275364800 is 2010-05-31 not 1st june 2010 in mysql's locale. Are these being run on different machines?

Categories