mysql timestamp not in epoch - php

I've set a field in my mysql db to "timestamp" yet it still returns time in queries as yyyy:mm:dd: time. I set it as a timestamp because I wanted milliseconds since 1970. How do I achieve this ?

What language are you using? In PHP, you can convert a MySQL timestamp to seconds from the epoch using the function strtotime. You could also, if absolutely desperate, use string parsing to get each part of the timestamp and calculate the seconds from the epoch yourself.

Just store the Unix timestamp as an INT. Then when you call it from the DB you can process it server side to get what you want.

Related

PHP time() function and MySQL fields

If I use the PHP's Time() function and in MySQL there are 4 fields DATE, DATETIME, TIMESTAMP, and TIME, which one I should use?
In PHP I use the Time() to record both the Date and the time like 5/10/2012, and the time is used to calculate the time elapsed.
Use what you need:
DATE:
stores only days ex: 2012-06-11
DATETIME:
stores days and time ex: 2012-06-11 12:49:31
TIMESTAMP:
stores days and time ex: 2012-06-11 12:49:31
MySQL has function that sets this field to current timestamp, when there was update in the row.
Maybe (don't know right now) can be specified by number. Others must be specified 'yyyy-mm-d hh:mm:ss'
to convert DB value to PHP's time use strtotime()
I would suggest you to use MySQL Data And Time functions instead. If you need to store current time, use NOW(). It's DATETIME type. Alternatively, you can use Unix Timestamp storing it as INT.
The Unix timestamp is the most basic form of a time/date- a "raw format", if you will. Once you have a timestamp, you can get to any other format you want. Personally I don't see the point in storing DATEs or DATETIMEs, only to convert it to a timestamp when you retrieve the data again, which of course you will need to do if you want to display a date/time in any readable format (see date() function).
MySQL has a field time that store the current timestamp when a record is created. Alternatively, and if you want more flexibility, PHP's time() function returns the current timestamp. PHP also has functions for calculating the timestamp at a certain point in time (e.g. if you want to specify a date in dd/mm/yyyy format).
So in summary, I would always use timestamps, and I recommend you do too, unless you have very specific needs.

MySQL convert DateTime (storing a UTC_TIMESTAMP) to number of seconds 1970

There are many similar questions out there but I believe this one is unique. (Sorry if it isn't)
Our database has datetime field named "date_sampled", of which we store with UTC_TIMESTAMP()
Our goal is to return the number of seconds since 1970. I noticed UNIX_TIMESTAMP() if supplied no argument returns the current UNIX_TIMESTAMP() and if a datetime (i.e. 2011-10-10) is passed, it returns a timestamp in seconds.
However UTC_TIMESTAMP() does not work like this, it Only returns a current UTC Timestamp.
So how can I convert my DateTime field (holding a UTC datetime) into the seconds from 1970 in MySQL? If it can't be done in MySQL, then a PHP solution will work.
Thanks.
There is a TIMESTAMPDIFF function in MySQL, you can use it something like
SELECT TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00', YourUTCDateFromSomewhere)
More details in the docs - http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampdiff
What were you trying to get UTC_TIMESTAMP to do that UNIX_TIMESTAMP doesn't? Unix timestamps are in UTC by definition.
I'm assuming the problem you're having is that, even though you're storing your datetimes in UTC, UNIX_TIMESTAMP is giving you a timezone-offset result, so you're getting a value several hours off from what you're expecting.
UNIX_TIMESTAMP respects MySQL's time_zone variable, so if all your dates are in UTC, you can just set your session's time_zone variable to UTC, which will cause UNIX_TIMESTAMP to do no timezone conversion when converting a datetime to a timestamp.
SET time_zone = '+00:00'
Interesting problem... I have no clue how to do this with SQL... but the PHP solution would be to use the strtotime() function..
<?php
echo strtotime('2011-10-10');
?>
The above example returns the value 1318219200 which is the number of seconds that have passed since the 1970 epoch

Fetch data from mysql by converting it's timestamp to unix time

This will be done with PHP.
I basically want to get the number of rows that were inserted 30 minutes ago.
I have a time field on my table which is type TIMESTAMP and on update it's set to CURRENT_TIMESTAMP.
The date is stored in this format:
2011-05-27 04:29:17
My query is supposed to look something like this, however i just can't do it
SELECT COUNT(*) FROM mytable WHERE UNIX_TIMESTAMP(time) < '.time().'-1800
Where time() is PHP's function that fetches the UNIX time.
What it should basically do is print me the number of rows inserted from now to 30 minutes ago, but i just can't seem to make it work.
Can somebody help?
Small edit:
Another problem i am seeing is that php's function time() displays the unix time which is UTC. The time stored in mysql is probably GMT i.e whatever my computer's time/timezone is set to.
You can easily get rows stored from now to 30 mins ago by simply using:
SELECT count(*) FROM mytable WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
Usage of UTC_TIMESTAMP is just an example if you're storing your date/time data as UTC_TIMESTAMP(), you can probably use NOW() if necessary, depends on what you're storing really.
**EDIT**
Removed bad pointers and fixed example :)
Do you really need your computer's timezone to be different than UTC? why not just set it to UTC & save yourself the confusion? If that doesn't work, just use dateadd() on mysql to convert your mysql timestamp to UTC when checking?
My suggestion would be to write a small function to convert the mysql timestamp to your PHP timestamp format & load it into mysql. Then all you need to do is to call tmstamp(time_stamp) instead of time_stamp in your query. You can do the reverse too i.e. Convert PHP's "30 minutes ago" timestamp to mysql format and rerun your query (probably easier).
Usually it's just a formatting issue. It's not standardized across programs.

PHP -Comparing Unix Timestamp To Now

I'm retrieving a unix timestamp from a DB and I want to check if this datetime has passed already.
I tried using an if statement to compare to time() but it always says the time has passed already.
What am I doing wrong?
EDIT: Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)
It's stored in the DB as int not as any datetime types.
Your PHP time could be affected by PHP's timezone. Use date_default_timezone_get() to find out what time zone you're in.
Make sure the timezones in the DB and PHP are the same, use NOW() function to fill the DB column with current timestamp (the column should be of datetime type), then you can get the timestamp using UNIX_TIMESTAMP() MySQL function which compares against PHP's time() just nice.
Alternatively, you can fill the DB column with something like
mysql_query("INSERT INTO your_table (your_date) VALUES (FROM_UNIXTIME(" . time() . "))")
That should work even with timezone discrepancies.
If you are using mktime to create a UNIX timestamp, PHP is using the timezone settings to interpret what you mean by the given parameters. It's possible that you should be using gmmktime. It depends on how the timestamps in the database are being created; I cannot say for sure without seeing more code and having a more detailed explanation.
I generally prefer to simply store all dates as DATETIME types in the UTC (GMT) timezone. It tends to be less confusing.
Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)
12 PM is hour 12.
1 PM is hour 13.
So you don't always add 12. (i.e., 12 Noon is the exception).

Convert MySQL UTC datetime to UNIX timestamp

These both correctly return the current UNIX timestamp:
SELECT UNIX_TIMESTAMP(LOCALTIMESTAMP()); #MySql
echo time(); //PHP
But I'm storing UTC_TIMESTAMPs in my database (not LOCALTIMESTAMPs).
How can I convert a UTC datetime to a UNIX timestamp using MySQL?
Note that LOCALTIMESTAMP() is a synonym for NOW(). So what you're really asking is how to get the current time and convert it to GMT and then convert to a unix timestamp to store in the db. So this will work:
SELECT UNIX_TIMESTAMP(CONVERT_TZ(NOW(), ##global.time_zone, 'GMT'));
As an aside, it's always much better to use the time and date columns of a database rather than unix timestamps. It makes querying and displaying results much easier.
Update: Are you sure you are getting what you think you are? UNIX_TIMESTAMP returns a UTC based seconds since the UNIX epoch. It does not return a MySQL DateTime type. If you have an actual UTC DateTime instance, then you can put that directly into your DateTime column of your database and don't have to use UNIX_TIMESTAMP as an intermediary. What type do you actually have that's in local time?

Categories