How to insert timestamp into mysql with php properly? - php

Here is my table num structure:
mysql> show columns from num;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| ip | char(20) | YES | | NULL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
I insert record with the following codes:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$time=$_SERVER['REQUEST_TIME'];
$db=mysql_connect("localhost","root","passwd");
$query="insert into num(ip,time) values('$ip','$time')";
mysql_select_db('numdb');
mysql_query($query, $db);
mysql_close();
echo "ok";
?>
The time is wrong after i inserted two records into table num,
What is matter with my database or php code?
mysql> select * from num;
+-----------+---------------------+
| ip | time |
+-----------+---------------------+
| 127.0.0.1 | 0000-00-00 00:00:00 |
| 127.0.0.1 | 0000-00-00 00:00:00 |
+-----------+---------------------+
2 rows in set (0.00 sec)

To insert the current timestamp, use:
$query="INSERT INTO num(time) VALUES (CURRENT_TIMESTAMP)";
If your server time is different from what you want, you can add hours (or minutes, etc) to the timestamp:
$query="INSERT INTO num(time) VALUES (CURRENT_TIMESTAMP + INTERVAL 6 HOUR)";

It looks like you want to use the REQUEST_TIME value instead of the current time. Fortunately, php makes it easy to convert the string format of the request time into an internal time format, then convert that to the
2015-03-14 15:19:27
standardized string format needed by MySQL to represent a timestamp. This line will give you the workable datestring.
$datestring = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
If you don't care about the distinction between the REQUEST_TIME and the current time, simply don't mention the timestamp column in your INSERT query, and MySQL will use the default (the present time).
Pro tip: Don't name a column timestamp because that's a reserved word. You have to wrap it in backticks in some contexts. Many developers use ts for this.

Related

query for time in mysql between hours

how to get a list of rows that is between 17:30 till tomarrow 8:30.
--------------------------------------
| id | user_id | action | time |
--------------------------------------
| 1 | 25 | enter | 1512459905
| 2 | 19 | exit | 1512125105
| 3 | 31 | enter | 1514581905 |
--------------------------------------
mysql table have a time column with unix timestamp and i want get a list every day that between 17:30 till 8:30
SELECT TIME_TO_SEC(TIMEDIFF(FROM_UNIXTIME('time 1'), FROM_UNIXTIME('time 2')) AS 'time_diff_in_sec' FROM 'your_table';
This can help:
Mysql Get Time Diff
$dateStart=strtotime(date("Y-m-d 17:30:00"));
$dateEnd=strtotime(date("Y-m-d 20:30:00"));
This is how you can convert your date into unix timestamp in php.
Then in your query:
SELECT * FROM my_table WHERE time BETWEEN '$dateStart' AND '$dateEnd'
If you don't want to execute your script manually every day you can set up a cronjob to do it for you.
You can use the TIME() function to extract the time part of the datatime,
So you can do something like this:
select * FROM table t
where TIME(f.time) between '17:30:00' AND '18:30:00'

MySQL compare dates in a column to today

I have a table of around 6000 records with a date column amongst other columns which represent the deadline for a query. I need to compare the date in the column to todays date which I understand is done something like:
SELECT DATEDIFF(DATE_TO_COMPARE, CURDATE());
However, I then have another comlumn I want to set to that date difference. So for each date, I need to compare, insert the difference in the column difference_in_days, iterate to the next date and repeat.
I am also invoking this function whenever a certain page on my site is loaded using AJAX and PHP/PDO
My SQL knowledge isn't that extensive, how can I achieve this.
Table is kinda of like
field 1, field2, field 3, date_to_compare, field 4, field 5, difference_in_days
| | | | 2016-04-20 | | | |
| | | | 2016-04-25 | | | |
| | | | 2016-04-22 | | | |
| | | | 2016-04-27 | | | |
| | | | 2016-04-29 | | | |
Sonds like you want to do an update?
UPDATE table_name
SET difference_in_days = DATEDIFF(date_to_compare, CURDATE());
This will update every record in the table to the diff of the current date.
However, this will require you running the update every day, if you want that column to maintain relevance.
Alternative Approach:
If you're not querying this a lot, you may be better off using a view, which will update real-time every time you query it.
CREATE VIEW diff_view_name AS
SELECT *, DATEDIFF(date_to_compare, CURDATE()) AS difference_in_days
FROM table_name;
Then you could query it using:
SELECT * FROM diff_view_name;

How to compare the current row to the next row of a mysql table in php

I want to compare the current row to the next to check if the third column of the current row is the same as to the next. If not, the current row index will adjust as the new current row and check again the next row. I want to do this in php. Can somebody help me.
UPDATED:
ID | Date | Time
------+-----------+-------
00091 | 2015-1-20 | 08:05
00091 | 2015-1-20 | 17:10
00099 | 2015-1-20 | 07:45
00099 | 2015-1-20 | 17:42
my expected outputs are:
ID | Date | Time
------+-----------+-------------
00091 | 2015-1-20 | 08:05 17:10
00099 | 2015-1-20 | 07:45 17:42
This looks like a terrible database design, so I would highly recommend changing it! Having duplicate IDs hurts me deeply.
But, here's a MySQL query that could get the result you are after:
SELECT `ID`, `Date`, CONCAT(MIN(`Time`), ' ', MAX(`Time`)) AS `Time`
FROM `test`
GROUP BY `ID`, `Date`;

How to generate an auto increment ID number with date and resets the next day in Phpmyadmin

I'm having a hard time figuring out on how to make a sequential number on my ID field together with the date today and then resets the next day with the date of the next day.
For example:
+------------+------+
| id | name |
+------------+------+
| 0322150001 | John |
| 0322150002 | Mark |
| 0322150003 | Josh |
| 0323150001 | Paul |
| 0323150002 | Bon |
+------------+------+
If you want that value to be implicitly set when inserting a row (i.e. without mentioning id in your query), the only way I know of is to set a default value for that field.
Quoting the MySQL Manual:
The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. [...] The exception is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP and DATETIME columns.
So your options are limited to either using CURRENT_TIMESTAMP and setting the field type to TIMESTAMP or DATETIME, or you set the id field in your SQL query.

mySQL datetime and timestamp

I want to update mysql rows where DATETIME < TIMESTAMP
DATETIME is like: "2014-06-21 17:56:00"
TIMESTAMP is like 1454546656 (which is now)
I want to update all the rows where DATETIME is in the past
What's the lightest method to deal with a huge number for rows?
Thanks.
In mysql there 2 function which are
from_unixtime() to convert the unix time to human readable date
unix_timestamp() to convert a human readable date to timestamp
So you can use one of then for the comparison
Here how it looks
mysql> select from_unixtime(1454546656);
+---------------------------+
| from_unixtime(1454546656) |
+---------------------------+
| 2016-02-04 06:14:16 |
+---------------------------+
mysql> select unix_timestamp('2014-06-21 17:56:00');
+---------------------------------------+
| unix_timestamp('2014-06-21 17:56:00') |
+---------------------------------------+
| 1403353560 |
+---------------------------------------+
mysql> select unix_timestamp('2014-06-21 17:56:00') < 1454546656;
+----------------------------------------------------+
| unix_timestamp('2014-06-21 17:56:00') < 1454546656 |
+----------------------------------------------------+
| 1 |
+----------------------------------------------------+
mysql> select from_unixtime(1454546656) > '2014-06-21 17:56:00';
+---------------------------------------------------+
| from_unixtime(1454546656) > '2014-06-21 17:56:00' |
+---------------------------------------------------+
| 1 |
+---------------------------------------------------+
So its upto you which one you want to use for the comparison.
Since you are using PHP, try using the PHP Date Function:
$ts = date("Y-m-d H:i:s", $timestamp); //Convert Unix Timestamp to MySQL Date/Time Format
UPDATE table WHERE DATETIME < '$ts';
This is the lightest method I can think of, because it is not recalculating the timestamp for each record, and furthermore, if the DateTime Field is indexed, it will go incredibly fast.

Categories