Mysql Unix Timestamp Store? - php

I got a bit of a problem here. My database stores unix timestamps as a string 2011-09-01 20:22:36 and I need it as a Unix Timestamp ########### so I can compare it using a > then symbol. But I also need to have it automatically set the timestamp on update (ON UPDATE CURRENT TIMESTAMP) as well as have a default of the timestamp which is not really that important cause I can do that in PHP if I need to.
How can I do this? timestamp is now a date/time combo string and not a integre so I cannot compare it?
My comparison string is
$sql = sprintf("SELECT nid, field_date_value, field_movie_location_value FROM content_type_mobile_event WHERE updated<'%s'", $vid);
Incase anyone is wondering.

Use the UNIX_TIMESTAMP() function to convert it inside your query. If you must compare it to a Unix timestamp from PHP, it is easiest to allow MySQL to handle the column's conversion on its end.
$sql = sprintf("SELECT nid, othercols FROM content_type_mobile_event WHERE UNIX_TIMESTAMP(updated) < '%s'", $vid);

You can compare DATETIME columns with operators like > or <, so I don't see what the problem is. For example, you can do this :
SELECT *
FROM table
WHERE your_column > NOW() - INTERVAL 2 HOUR;
If you really need unix timestamps (you shouldn't, it's a bad habit PHP users have), you can use the function UNIX_TIMESTAMP :
SELECT UNIX_TIMESTAMP(your_col)
FROM table;
You can also use FROM_UNIXTIME to convert a unix timestamp to a valid date :
SELECT *
FROM table
WHERE your_column > FROM_UNIXTIME($data)
This is what I would use if I really had to use a unix timestamp, but most of the time, you can do without.

Related

How to compare Firebird Database TIMESTAMP with PHP time() value?

My database has a data field of type TIMESTAMP. It's called "mytime". I get a number from php's time() function. e.g:
print time();
Then I query the db:
SELECT * FROM mytable where mytime > 1369157557
It complains:
conversion error from string "1369157557"
I'm guessing you can't compare a PHP timestamp with a Firebird timestamp. Why not? And how do I get around it?
There are two options: convert that unix timestamp to a Firebird timestamp before comparison. Or do something like described in this message on the Firebird-Support list:
select DATEDIFF(second, timestamp '1/1/1970 00:00:00', current_timestamp)
from rdb$database
Or specifically in your query:
SELECT *
FROM mytable
where DATEDIFF(second, timestamp '1/1/1970 00:00:00', mytime) > 1369157557
Just keep in mind that the times stored in your database may not be stored in UTC, and in that case you need to take the offset against UTC into consideration as well.
You can use the UNIX_TIMESTAMP function. Be aware though that php and mysql may disagree, both a few seconds-wise as timezone-wise.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
SELECT * FROM mytable where UNIX_TIMESTAMP(mytime) > 1369157557

Converting UK date to UTC

I have a table called pm_msg with a time column and the following values:
07-02-2013 18:11:00
27-01-2013 16:02:44
28-01-2013 10:30:26
30-01-2013 13:30:06
I would like to convert them to Unix timestamp while running an PDO SQL query.
This is what I've done so far, but it seems to return an error. How should I go about doing it?
$sql = "SELECT * from pm_msg ORDER BY (strtotime(time)) ASC;";
strtotime does not exist in MySQL, you want the UNIX_TIMESTAMP function:
$sql = "SELECT * from pm_msg ORDER BY (UNIX_TIMESTAMP(time)) ASC;";
But - you don't need to convert it to a UNIX timestamp just to sort it. Sorting works correctly on dates? Maybe you intend to add the UNIX_TIMESTAMP to the SELECT portion?
UTC is more or less the same as GMT - does not take into account of DST.
So in spring and autumn you need to take that into account.
But to convert a date/time into unixtime using SQL see STR_TO_DATE or UNIXTIMESTAMP
It would be easier to setup your time column as datetime, if you are using something like varchar right now.

Mysql timezone and selecting rows from one day

I use MySQL DATETIME column to store date & time. Dates are in UTC. I want to select item from one day. What i'm doing now:
SELECT * FROM data WHERE DATE(CONVERT_TZ(datetime, 'UTC', 'Australia/Sydney')) = '2012-06-01'
note that the timezone depends on user
Problem is that it is quite slow with table growing.
Is there any solution how to make it faster?
Currently your query has to compute the conversion for every row of the database. You probably could make things better by converting the other way round, so that the conversion only occurs once (or actually twice, as you'll have to form a range). Then a proper index on datetime should make things pretty fast.
SELECT * FROM data
WHERE datetime BETWEEN CONVERT_TZ('2012-06-01 00:00:00', 'Australia/Sydney', 'UTC')
AND CONVERT_TZ('2012-06-01 23:59:59', 'Australia/Sydney', 'UTC')
Or if you worry about a 23:60:00 leap second not getting matched by any query, you can do
SELECT * FROM data
WHERE datetime >= CONVERT_TZ('2012-06-01', 'Australia/Sydney', 'UTC')
AND datetime < CONVERT_TZ('2012-06-01' + INTERVAL 1 DAY, 'Australia/Sydney', 'UTC')
In the latter form, you wouldn't have to add the hours PHP-side but instead could simply pass the date as a string parameter.
Depending on your real goal, using TIMESTAMP instead of DATETIME may be a good solution.
TIMESTAMP stores the datetime as UTC, converting as it stores and as it is fetched, from/to the local timezone. This way, what I read from your table is automatically different than what you stored (assuming we are in different timezones).
Yes, use #MvG's approach of flipping the query. Yes, use his second form.
And index the column. In composite indexes, put the timestamp last, even though it is more selective.
DO NOT do SELECT *
Indexing - make sure apropriate colunms/id
fields are indexed.
Do time-conversion php-side.
OR make sure you do 1 & 2 and it may be wrapped into a Stored Proc, passing timezone as param.
Currently MySQL query will be as below:
SELECT * FROM data
WHERE datetime >= CONVERT_TZ('2012-06-01', '+00:00', '+10:00')
AND datetime < CONVERT_TZ('2012-06-01' + INTERVAL 1 DAY, '+10:00', '+00:00')

PHP, using timestamp for mysql?

I've been trying to search for a straight answer, but for some reason the answer isn't coming to me. I was wondering what is the best way to store date/time into mysql?
I researched that timestamp in mysql is good because it will update depending on timezones too.
So I've set my column name as timestamp with datatype in mysql to timestamp, but what is the best syntax for storing current date/time to that?
"INSERT INTO table(timestamp) VALUES(now())" //or use timestamp()? or is there such thing?
?
Please help, thanks!
You could use datetime datatype in your table and just store it as NOW() which will store it as YYYY-MM-DD HHHH:MM:SS
Here are some considerations:
Set the column type to BIGINT so you can store 64-bit timestamps
You can insert using the PHP time() function, or MySQL's UNIX_TIMESTAMP() function
Name your column something other than timestamp. It is a permitted word1 see last section, but its also a type in MySQL.
If you store times as a Unix timestamp in MySQL, they are stored in UTC which makes dealing with timezone conversion very easy. In a 64-bit environment, PHP can handle dates up to the year 219,250,468.
Both of these queries are the same:
INSERT INTO `table` (`time`) VALUES(UNIX_TIMESTAMP());
// or
$time = time();
INSERT INTO `table` (`time`) VALUES($time);
Then to display it in PHP:
SELECT `time` from `table` WHERE `id` = 1;
echo date('Y-m-d H:i:s e', $row['time']); // 2012-07-31 23:59:59 America/Los_Angeles
Whatever timezone is set in PHP date_default_timezone_set() will be the timezone used when you output the date in PHP.

Convert Date Format

I have a moodle installation in which there is a column in mdl_user table called firstaccess whose type is bigint(10) and contains date in following format 1266839570.
I am writing a query for accessing users according to date filters. For e.g. i want to check which users firstaccess is greater than '2010-04-12'. How can i convert the date? These two date formats are different. I think firstaccess is unix timestamp. Should i change the '2010-04-12' into unix timestamp or there is a way to convert firstaccess i.e 1266839570 to yyyy-mm-dd format.
Please help me on this.
Thanks
You can create a unix timestamp in php with the mktime() function, then simply put it in your query.
MySQL has a date_format() function, that can format dates however you like, but I'm not sure if it works with bigints. You'd better go with the mktime.
date() and mktime() are functions to concert from unix timestamp and back.
You can convert your dates in either way
I believe you can write your query using a timestamp. Eg.
SELECT * FROM mytable WHERE firstaccess >= TIMESTAMP('2010-04-12')
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I don't know what form the date in your form is, but you can easily convert it to a timestamp (if it already isn't one) using mktime. For example:
$mytimestamp=mktime(0,0,0, $month, $day, $year);
Then just add it to your query:
$myQuery= "SELECT whatever FROM sometable WHERE " . $mytimestamp . ">=firstaccess";
Like Paul Peelen, my answer is a MySQL query. I'm going the other way, though, and converting first access into a date.
Using the date information in your problem:
SELECT * FROM mytable WHERE DATE_FORMAT(FROM_UNIXTIME(firstaccess), '%Y-%m-%d') > '2010-04-12';

Categories