So when user are created in drupal website it creates a table row in SQL database, everybody knows that, but there is column created witch stands for create date. It contains int type, something like this 1341319209. I checked, it isn't the milliseconds from 01.01.1970.
The question is how can I convert it to normal date format using php or SQL Query? I tried to find how drupal does that, bet no success. Can someone please help. Write some code or give me a clue. :) Thanks.
You may try this:
MAKETIME( seconds / (60*60),
seconds / 60,
seconds % 60 )
or you may also try to use FROM_UNIXTIME()
SELECT
from_unixtime(timestamp)
FROM
your_table
Related
My goal is to make a column in phpMyAdmin, which counts down the remaining days of a trial period (or something like that).
So for example when I set remainingDays to 30, I want the database to execute the query every 24 hrs
Is it possible to make something like this with only phpMyAdmin in hand, or do I have to put some code onto my website to send MySql Commands to subtract it?
Any help is greatly appreciated.
A better approach would be to have a startDate column compute the remaining days during query execution with something like:
select 30-datediff(now(),startDate) from...
Are you familiar with datediff()? Something like:
select datediff(target_date, curdate()) as days_remaining
from t;
That is, you can do this in a SQL query.
Based on the solution here I tried using:
CREATE EVENT delete_expired_101
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 24 HOUR DO
DELETE FROM my_table WHERE id = 101;
on my php query, but it doesn't seem to work and gives me invalid syntax. Can you recommend me a way for this?
Take note I am using MS SQL not MySQL.
You need to use SQL Agent. The logic could be a SQL Agent job that is fired every 24 hours that deletes any data that has a datetime less than DATEADD(d,-1,getdate()). Please do some research on how to implement SQL Agent. If you need help with the code to delete data in a table based on a datetime value, please open a new question that includes that table's schema.
Ill be choosing #Chris H's suggestion by using delete function and set a table column which records timestamps.
Thanks for all the help
I am creating a mysql db with a php frontend. The data it will use is extracted from another larger db and contains a date/time field which looks like this - 20120301073136 - which records when an event happened.
I understand that this might be a UNIX timestamp? Not sure.
I want to be show this field in the tables in my PHP webpage as a readable date and time -
ie something like 01-Mar-2012 07:31:36 or similar
Should I try and convert it with SQL command or let PHP format it? And, what is the code to do so?
BTW, it is important that I can sort the data (in SQL and in the PHP table) into date order - ie in the order that these events happened.
Thanks in advance for your help - Ive learnt a lot here already
J
You can convert it to a datetime directly in your SQL query. Example:
select cast(20120301073136 as datetime)
You can also order that with no need to convert it since it is a number in the format YYYYMMDDHHmmss
select * from yourTable
order by yourDateTimeField
You should make use of the MYSQL DATE functions. Check the docs before asking simple questions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
Also you can sort the dates directly in your query using ORDER BY.
I have a mysql database table that gets the "datetime" column automatically filled when a user fills in the php form page.
$timestamp = datetime("Y-m-d H:i:s");
Trouble is, the database server is in the USA which is 9 hours 30 minutes behind my time.
Does anyone know a way to change this so that the 9 hours 30 minutes hours get added to the datetime?
To answer your question directly, you can use the following function in your select statement:
select date_add(datefield, interval 570 minute) from table
However, as stated in some of the comments it's best if you correct the problem from the source and update the timezone in your DBMS.
You could always just use date which has an argument for timezone.
http://www.php.net/manual/en/function.date.php
There are several possibilities. See http://www.php.net/manual/en/ref.datetime.php for a list of DateTime-Functions. date_add and date_sub are good candidates to start with
date_default_timezone_set().
1) Set the correct timezone in the php.ini file.
2) Set the correct timezone in your DBMS ( MySQL timezone change? for MySQL)
This won't require you to add some weird parameters or constantly use the same functions in your scripts.
So what I'm doing is storing data from a website every 4hrs. I want to have a line graph of the last two days, the y-axis would be number of players and that value can be anywhere from 0-30,000, the value is dependent on the scrape of the website.
What is the best way to store the data in mysql and where is a easy to use graphing solution?
Has anyone used Raphaƫl?
Hey, Google Charts is exactly what you're looking for. It can create any type of chart from a data set, and is very customizable.
As for the actual data retrieving, the answers above will help you. :)
You could have a table structure like
Player_Stats
players int
hour int
Then each hour you could write something like:
insert into Player_stats (players, hour) values(NUMBEROFPLAYERS, HOUR#);
Where HOUR# is a value from 1 to X number of possible hours (if you want to only store things in a running log, otherwise, change hour to a timestamp)....the insert would be more like
insert into Player_Stats (players, timestamp) values(NUMBEROFPLAYERS, NOW());
Then you'd retreive your data with:
select players, hour from Player_Stats;
Or if you kept things in perpetuity and wanted to grab a range from now to 2 days ago:
select players, timestamp from Player_Stats where timestamp between now() and date_sub(timestamp, interval 2 day);
Then you could use a charting library like Google Visualizations...they have good documentation on formatting the data specifically for their different charts.
I've done something similar. I stored the number of players along with a time stamp in a table, then used jquery and jqplot to display the data.
I would set up a cron to run a SELECT TO OUTFILE myFile statement regularly. Note that myFile cannot be an existing file for security purposes (docs), so you'd have to have the cron also delete the file after the plot is created.
I have found ploticus to be very easy to work with, and can make some very complex plots without too much difficulty.