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.
Related
I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.
MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51
I have a PHP MySQL query that inserts some data into a MySQL database and it includes a timestamp.
Currently the INSERT query uses NOW() for the the timestamp column and it is saved in the database in the following format: 2012-07-24 13:13:02
Unfortunately for me the Server is not in my time zone and it is listed as America/Los_Angeles as shown print date_default_timezone_get();
I was hoping to do the following:
date_default_timezone_set('Europe/London');
$timefordbLondonEU = date('Y-m-d H:i:s', time());
and simply save into the database the $timefordbLondonEU in place of the NOW();
Is this a good way to save such data ?
Many Thanks,
Richard
[ADDED TEXT]
I changed the Type in the MySQL db to DateTime and did the following:
date_default_timezone_set('Europe/London');
$timefordbLondonEU = date('Y-m-d H:i:s', time());
It is working but Im still not getting the overall concept yet.
Assumptions based on your comments:
MySQL = Does not have a datatype UTC you simply use type INT.
Unix_TimeStamp() will save the current time or count? in UTC format such as 1343247227.
As UTC is a count from a common 0 point you can get any timezone from it. Assuming that you don't want a date before the reference 0 point in 1970.
My guess and lead on from what you have said is the best way to do it is save the time as UTC in an INT (1343247227) and then generate any time zones you want from there. Again assuming you don't need to store dates before the reference 0 point in 1970.
Equally why not store as datetime YYYY-MM-DD HH:MM:SS at a known timezone and then convert to UTC or other timezones. It all seems pretty messy =(
As #Petah said in the comments, store your times in UTC and covert them in the application as needed.
Unix timestamps are in UTC so I usually store my times in the database as timestamps. This saves the headache and confusion of first converting to UTC to insert, and then from UTC when selecting.
That is, make your time field an INT type, and use the function UNIX_TIMESTAMP() in MySQL when you insert, or get the timestamp from PHP using the time() function.
When you fetch the timestamp from the DB it will be in UTC, but when you display it in your PHP application using date(), it will display in the server timezone, or whatever you set with date_default_timezone_set.
Therefore the following two queries will work:
INSERT INTO `table` (id, time) VALUES(NULL, UNIX_TIMESTAMP());
// or
$time = time();
$query = "INSERT INTO `table` (id, time) VALUES(NULL, $time);
If you want to select it from the DB as a DATETIME, you can do this:
SELECT *, FROM_UNIXTIME(time) as dt FROM `table` WHERE 1
The resulting dt column will be in the format yyyy-MM-dd hh:mm:ss.
You can format the numeric timestamp in PHP using date()
If the PHP version you have is 64-bit, you aren't limited to the 1970 - 2036 range, PHP will support 64-bit timestamps, just make sure to use a BIGINT column in MySQL in that case.
Hope that helps.
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.
I have a column that is populated with a date format that is useful for another application but not the mysql format for timestamps. Here is what it looks like:
2010.01.28 12:00 ("time" column) instead of the mysql timestamp: 2010-01-28 12:00:00 ("updatetime" column).
I need to do a date specific search between two time periods and for a normal mysql timestamp I would run this:
SELECT * FROM table WHERE updatetime BETWEEN '$dateStart' and '$dateEnd'
but this doesn't work with the "time" column formatted as it is. I would prefer to keep that column formatted as such as a different application requires that date format, so does anyone know a MYSQL way to search BETWEEN two timeperiods with the 2010.01.28 12:00 format? Running from a PHP script
You can either format the times in the same format in PHP, or in MySQL
WHERE updatetime BETWEEN
date_format('$datestart','%Y.%m.%d %H:%s')
AND date_format('$dateend','%Y.%m.%d %H:%s')
You might want to try something like this
SELECT * FROM table WHERE STR_TO_DATE(updatetime, "%d.%m.%Y %h:%i")
BETWEEN '$dateStart' and '$dateEnd'
to convert the string into a date - that is if the values of $dateStart and $dateEnd are already in the correct format for comparison with a sql date
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';