Use the UNIX epoch in a mysql column? - php

A column in one of my tables is DATE and currently it gets automatically filled with a timestamp of when the row was made. The timestamp looks like 2013-02-10 19:56:12. Is there anyway I can have it timestamp with the unix epoch automatically? I hope this makes sense. I'm still a beginner when it comes to mysql.

if you want to project (select) TIMESTAMP into UNIX format, use UNIX_TIMESTAMP(date)
SELECT UNIX_TIMESTAMP(date)
FROM tableName
UNIX_TIMESTAMP() MySQL Docs
SQLFiddle Demonstration

You could use the unix_timestamp function when retrieving data:
SELECT unix_timestamp(timestampColumn)

Related

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.

Inserting date to database in mysql

I use NOW() function but I get this weird date:
2011-11-06
How do I get the following European date:
ss:mm:hh dd:mm:year (06-11-2011)
In my database I set the field column date as DATE
How do you integrate DATEFORMAT into this query:
$avatar_Q=mysql_query("
SELECT user_name,avatar,reputation,comment, DATE_FORMAT(date,'%d/%m/%Y %h:%i') AS rightNow
FROM comments AS com
INNER JOIN users AS us ON com.user_id=us.user_id
WHERE comment_id=$commentID
") or die(mysql_error());
The date is is in the column table
MySQL uses the following date format - YYYY-MM-DD - if you want a different format you need to use the DATE_FORMAT function on select
for example :
SELECT DATE_FORMAT(datecolumn,"%d/%m/%Y %h:%i")
FROM atable
To integrate the date_format function into your select statement you need to list the fields individually
Yep, use a Timestamp column and insert with CURRENT_TIMESTAMP. It saves a lot of time! :)
My personal recommendation is to save the date as a UNIX Timestamp (using the time() function) ,
This way you could format it as you wish .
Shai.
DATE_FORMAT
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
That format is just how MySQL stores the data type DATE: http://dev.mysql.com/doc/refman/5.5/en/datetime.html.
If you're using the DATE data type for you column you can either:
Use the DATE_FORMAT function in your SQL to get the date in your desired format
Use a combination of PHP's strtotime and date functions to display your date in the most appropriate format

Mysql select - Get the dates of timestamps

At the mysql table, there are stored values of timestamps (like 1265138145).
What i want is to display the dates (eg 27/2/2011,10/3/2011,15/3/2011, 16/03/2011 etc) which belong to these timestamps. Is this possible?
(but only display one time the date, eg if there is 1265138145 and 1265138140 then display only one time the date - which is 16/3)
There are a variety of ways of doing this, the FROM_UNIXTIME command probably being the easiest.
For example: SELECT FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y');
I'm not sure what you mean about "only display one time the date", but using DISTINCT on the necessary column should help.
i.e.: SELECT DISTINCT(FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y')); may be all you require.
From within MySQL, use ADDDATE and interval of unixtimestamp seconds to the epoch, e.g.
select adddate('1970-01-01', interval 1265138145 second)
then display only one time the date
Use DISTINCT in your query, e.g.
select distinct date(adddate('1970-01-01', interval 1265138145 second))
from tbl ..
Both queries above return the column as a datetime value, which you can apply default formatting to in PHP.
Note about using FROM_UNIXTIME - you get your local UTC offset added to the time, which is unlikely to be what you want, unless the data was populated using UNIX_TIMESTAMP in the first place.
FROM_UNIXTIME: Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone
<?php
print date("d/m/Y", $timestamp);
?>
http://us.php.net/manual/en/function.date.php
Fetch your data and use just date() function.
echo date('d/m/Y', $row['date']);
use FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
in mySQL statement

sql - BETWEEN timeframes with a weird timestamp

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

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