How to count remaining days in MySQL - php

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.

Related

Estimate and add missing values in MySQL table

Hi I have a MySQL table of Facebook pages (fbpagesfancount) that has the total fan count by day since 01 Jan 2016.
The structure is like this:-
Pageid, Pagename, Updated_Date, Fan_Count
There are a number of specific days that are missing and do not therefore have fancount values due to Facebook API issues.
The days that are missing are usually single days, for example, there is a value for the day before and the day after.
I'd like to create a new table that has a record for every day since 01/01/2016 for each page (750 pages) and then update the days that are missing by averaging the day before and the day after the missing date.
Is this possible using MySQL only or should I write a script in PHP that performs this task and if so, any suggestions on the logic would be helpful.
Any other suggestions on how to tackle this issue would be welcome.
Thanks
Jonathan
Yes, it is possible in SQL only.
No, you should not attempt it as it is more complicated and for a single shot there's no need.
Yes, write a script in any language you know, for instance PHP.
I'm not sure why you even want to create a new table? You could add a flag to your current table saying its an origional count vs an average, and just find the missing numbers and add them in a script.

Proper MYSQL syntax for timestamp comparison and distinct results with certain exclusions

Sorry for asking, but I've never had to do such a complex MYSQL query before and I don't actually know what to google search in order to get the answer.
I have a poorly crafted database with a table of appointments of pregnant women that includes the day they came and the number of weeks pregnant they were at that time. I'm trying to select each one that should be 30 weeks right now but that doesn't already have a separate entry after 25 weeks pregnancy. I use the phone number to uniquely identify each person.
Since I really don't know how to formulate this query, this is the best I've come up with.
SELECT * FROM patientlist WHERE
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`) - `weekspreg`*604800) > 29*604800
AND
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`)- `weekspreg`*604800) <= 30*604800
AND
/* a subquery that keeps out results where the phone number would show up elsewhere in the table for a woman with more than 25 weeks of pregnancy. */
There has to be a better solution than separately querying each of the results from the date range by phone number to see if the weekspreg is more than 25.
Thank you in advance for any help or direction.
Your entire WHERE is incorrect. A query can only have ONE where clause. You join multiple conditions with and and or, not and where:
WHERE foo AND bar // correct
WHERE foo AND WHERE bar // syntax error
Check out the MySQL Date and Time Functions. For example, I'm not entirely certain what that last WHERE clause is trying to do, but I believe the first portion could be rewritten as something like:
SELECT *
FROM patientlist
WHERE `date` - interval `weekspreg` week
between now() - interval 29 week
and now() - interval 30 week

Drupal created timestamp in SQL?

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

MySQL while for a date loop

I looking to run a mysql query and have the number of matching lines for a series of dates be added to an array. To better illustrate this, I have a database with dates, a clients phone number and info for a particular call. I need the run a query that looks and each day for the past thirty days and put the total number of calls for that client on that day and adds it to an array. So if my client had 20 calls today, 15 yesterday, and 10 the day before continuing for 30 day, I need an array that looks like this [20, 15, 10 ...].
I have this so far but can't figure out how to make it repeat for each day and add it to an array.
"SELECT COUNT(*) FROM db WHERE inbound='$client' AND calldate=$date(*this may need to go)"
Thanks for any help
You can use GROUP BY calldate to resolve the count by call date:
SELECT calldate, COUNT(*) FROM db
WHERE inbound='$client'
GROUP BY calldate ORDER BY calldate DESC
I am adding calldate to the query for your safety. Imagine the client did not call yesterday, then you don't want to skip that day. Make sure to check the calldate from within your php code.
EDIT:
Here is a blog post for you that shows an example of how to loop through a result set in php:
http://blog.mclaughlinsoftware.com/2012/07/14/phpmysql-query/

Table with events unixtime to day statistics

I have an online dictionary. All inputs are saved in table "id-word-unixtime". Now I want to make statistics of how many words were searched in an day, and then make graph or table with that data.
What is a best way to archive this? Should I make another table for dates or write a PHP script?
I just need basic direction.
Many questions there, but the main thing you seem concerned about is getting dates from unixtime.
MySQL has FROM_UNIXTIME().
Something like this should get you started:
SELECT FROM_UNIXTIME(unixtime,'%Y-%m-%d') as the_date,
count(word) as word_count
FROM table
-- add conditions here
GROUP BY 1;
If you have PHP-specific questions regarding data presentation I suggest you open another question.
You probably want to compute the answer once and then cache the result, to reduce the load on your server. Once the day is over, you only have to calculate all the statistics for it once.
Something important to thing about is when a day begins and ends. If your site has visitors form all over, you should probably use UTC. If 95% of your visitors are from the US, Eastern Time might make more sense.
SELECT COUNT(*), FROM_UNIXTIME(unixtime, '%Y-%M-%D')
FROM table
WHERE unixtime BETWEEN $start AND $end
GROUP BY FROM_UNIXTIME(unixtime, '%Y-%M-%D')
This should give you each day with searches per day. It's quite an expensive search, so you may want to cache it.
You're right, it's a very basic SQL query. Something like
SELECT word, count(word) FROM table
WHERE unixtime BETWEEN (starttime AND endtime) GROUP BY word
You can calculate starttime and endtime in either PHP or MySQL.
And sure, you will need to write a PHP script to draw a graph, but it's another question.

Categories