Use of timestamp from database in jquery - php

I am trying to run a counter from the time user is entered into database
I got this fiddle
http://jsfiddle.net/brkp1sa2/
which starts timer from 08/24/2012 while i need to start it from user date which i enter into database as timestamp at the time of signup
How I can do it as I fetch val from database like
<?php $timd = $db->fetchVal("select ts from users where id = ?", $id);
if (!empty($timd)) {
$timdl = $timd->ts;
}
Not know php pr jquery much so a code example answer can help me better
How to use this value into jquery so time start from given time stamp

Javascript timestamps are Javascript numbers representing Unix time in milliseconds. MySQL uses its UNIX_TIMESTAMP() function to generate Unix timestamps (in seconds) from various kinds of date / time datatypes.
So, the query
select UNIX_TIMESTAMP(ts) * 1000.0 AS ts from users where id = ?
will generate a Javascript timestamp value.
Now, Javascript timestamps and Unix timestamps are, by design at least, in the UTC time zone. Depending on how your table's ts values were stored, your results may come out in local time.

Related

How to deal with time zone in MySQL?

I have a script which has to do a "Google Analytics" like task.
Basically display visitor statistics for a user.
I want to generate a report in the time zone of the user who is requesting it.
So far I have written a code with this:
SET time_zone = timezone;
What it does it sets the time zone per each MySQL connection. If a user retrieves data with timestamp the timestamp is converted to the timezone of the connection. I am storing the UTC in the timestamp.
So everything seems to work. But some people are saying that this is a wrong approach. Because multiple user can't connect to the database with different time_zone setting.
But the MySQL doc says:
Per-connection time zones. Each client that connects has its own time
zone setting, given by the session time_zone variable. Initially, the
session variable takes its value from the global time_zone variable,
but the client can change its own time zone with this statement
However they keep insisting that you should not do anything with time zone in MySQL at all. You should do it all in your (for example) PHP code.
Here a similar question with this answer.
But how can I do it in the PHP code? I mean I know how to convert a time with a time zone in PHP but it's not like I am retrieving a single row.
I am retrieving thousands of rows and GROUP them by the date in the timestamp field:
SELECT ...
FROM logs
WHERE
user_id = :user_id
AND timestamp >= CURDATE()
GROUP BY DATE(timestamp)
It is very important that MySQL is using the index of timestamp because I have millions of. Does the index work even though I am using a function on the timestamp GROUP BY DATE(timestamp)? If not how else could I accomplish this?
So how should I do this all? I would be really thankful for some advice.
Now User converts all timestamps to his tz and uses timestamp(*user) >= CURDATE(*user).
I think the other way is to
convert timestamp(user) to timestamp(server)
and use
timestamp(*server) >= CURDATE(*server)
example
dates =(5,6,7)
(convert to my tz) dates-2 = (3,4,5)
(check constrain directly with this array )biggerThan3 result=(4,5)
2nd way
(convert constraint to ts server) biggerThan3 -> biggerThan(3+2)5
(check constraint with server array) dates =(5,6,7) result=(6,7)
result can be converted to (4,5)

Php/MySQL Auto date checking/comparing

Wanted to ask You how can I setup something on my php website, that would everyday automatically check and compare current date to all the database datetime entries and delete the rows of the dates that are in the past (for ex. if the current date is 2014-03-17, it would delete the rows that have datetime of 2014-03-16 ).
Because I basically have a TV-package website (not a real thing, just for a class), where you can order a package, you enter for how long and it adds that amount to current date, writes the order into database with the date written into a field named "expires". Would it make sense if I just wrote the checking function into the index, so when someone visits the site it would delete it? If so, how could I compare the two dates?
The DB example looks something like this: http://s29.postimg.org/7sbgj2hnr/dbtest.png
Although I highly recommend a scheduled task, you can do it in PHP by calling:
$sql = "DELETE FROM tableName WHERE `expires`<'".date('Y-m-d')."'";
Convert the date to a unix timestamp and compare it against the value of time() like you would any other integers.

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.

Adding values to a Timestamp

I am trying to create a script for quiz. I have stored current timestamp in MySQL DB as quiz start time, and want to check after each quiz how much time is left.
I have the idea that I will add 30 mins to saved time stamp and subtract this value from current time. That will be the time left. But I don't know the exact way of doing this.
My time stamp is saved in DB in format 2010-08-24 20:08:59. Any one have the idea.
Please let me know if someone have done it, or know how to get it.
Adding 30 mins to time stamp and showing the user how much time is left.
I am using the now() function to store the timestamp in DB.
Thanks
I would personally store the output of PHP time() in the database.
If you a human readable format from this value, you could use date('Y-m-d H:i:s', $fromdatabase);.
You want to store an actual UNIX timestamp in the database, not a string in that format.
You may or may not be doing this already, it depends on the type of column you're using. For MySQL, you should be using TIMESTAMP, which allows you to retrieve the timestamp with
SELECT UNIX_TIMESTAMP(column_name) ...
To store the current time + 30 minutes, all you have to do is:
INSERT INTO table (column_name) VALUES(UNIX_TIMESTAMP() + 1800)
You can know if the time has expired by comparing time() against the value of the column.

Maybe a dumb question on timezones

I have confused myself to a point where I need to double check this with you all. Sorry if this is really a dumb question..
Okay - I have a PHP web application running on Linux that needs to support timezones. I provide a calendar widget that allows the user to set their timezone then select the date and hours/minutes for some event.
I then store that value in a record as an int by using. When I need to show the date again to the user I just get their timezone setting and apply to the int value.
Code:
$dtzone = new DateTimeZone($time_zone);
$dtime = new DateTime($date_time, $dtzone);
$timestamp = $dtime->format('U');
return $timestamp;
Notes:
$date_time comes in this format 'YYYY-MM-DD H:S'
$time_zone is what they selected eg: America/Jamaica
Now I store the int returned in the database.
I then have a PHP script that I execute via CRON which runs within a server in Sydney - Australia.
My understanding is that all my script needs to do is get the servers current time stamp via time() then search the database for any integers with the exact same integer produced by the time() call. I don't have to worry about what timezone the user used to select the date/time.
Note that the CRON must run every minute as I allowed the user to select the minute as well.
Is this assumption correct?
Cheers
Marc
If you're storing the timestamps as int then you're most likely using unix timestamps which are always UTC. A unix timestamp is the same no matter where it is generated. Be it Jamaica, Sydney, or Mars.

Categories