I need to remove all records in my table when time is 23.59, I have the following query:
function delChat() {
$date = date('H:i');
$this->db->where(dimasukkan, '23:59');
$this->db->delete('chat');
}
but nothing happens
Please be aware that I don't know PHP, so I can't provide working code. However, I'll try to help out.
First, your question is ambiguous. Do you mean that you want to remove all records from the table every day when it is 23:59 h, or do you mean that you must remove every record whose timestamp is 23:59 h?
If you want to empty the table every day when it is 23:59 h, a cron job would be the right thing (as some comments already propose).
The cron job could be used on Linux / Unix systems; an alternative to that would be at. Under Windows, you could use the task scheduler for regularly running your job.
In every case, please be aware that you actually can't predict how long the operation will take. Possibly it will still run when it is already 00:00 h, or even 00:01 h the next day, which will eventually get you into trouble.
If you want to delete every row whose timestamp or date column is 23:59 h, your code has some problems:
Your where statement is wrong. You are comparing a normal variable to a constant here. I have no clue what PHP / MySQL are doing in such cases, but it certainly is not what you expect. If you want to compare e.g. a date column to that constant, you would have to it that way: ... where('date', '23:59').
Almost certainly, your date column or timestamp does not contain the date / time in the format needed. Hence, if you want to compare hour:minute to your date or timestamp column, you would first have to extract the hour and the minute from it and concatenate that to the format needed (or perhaps MySQL already provides an appropriate function for that), and then compare the result to your string constant.
Related
I'm a beginner for php and developing this web application, which users who registered on this site, can be claimed some scores in every one hour. When a user claims at some time, database stores that time as time data type in to user_claim_time column. When that same user tries for his next claim, this php script be able to get his last claim time and add one hour to check if the user really claims in an one hour.
So, my question is how can we add one hour to queried time. I'm using php time(h:i:s) function to store server's current time into the database.
You can do something like this:
SELECT * FROM your_table
WHERE user_claim_time < NOW() - INTERVAL 1 HOUR
However i recommend you to use user_claim_time column in datetime format.
Because time like this '00:00:00' will produce negative output as one hour subtraction can change the date or month as well. For example date like this '2017-08-01 00:00:00'.
So using datetime is the right way i think to properly compare time difference.
I need to set the current date and time in static variable.
I need to insert the 50 records into database table. Here,I need to insert the current date and time. Then, I need to set the current date and time of the 50 records are same. I used this date('Y-m-d H:i:s'); format. This format will change every minutes and seconds.
How to I do. Please help me.
$date = date('Y-m-d H:i:s')
then use this variable,for create records. All records will have same time.
It's been a long time, but I found a simple solution. Maybe it's useful for someone (like me) that wants to do the same thing nowadays.
Even when you store the current time/datetime to a variable, the time it's still running, so it changes every second.
I solved it by storing the time() value into a MySQL table (datetime type of course), so the captured time will be stored as it is and stop running and changing every second. Then, when I want to use it, I just make a query from the MySQL table.
It's a simple way (for me) to capture the "now" value and make it stop running, but maybe there's a better way.
I'm creating a report in php in which 6 html drop downs appear and prompt the user to enter the two dates in which they would like to see the data of the report. So for example the report goes as follows:
See data between: [month][day][year] and [month][day][year] (where the brackets signify a select tag)
Also in this report is a function which calculates the percentage increase or decrease from the previous day. So for example if the user does not select any date range, it's simply data of the current day and the percentage is calculated as:
round(((($newDataPointCount - $yesterdayDataPointCount) / $yesterdayDataPointCount) * 100),2)
This is obviously very easy to calculate for only one day because I can tell it to query the SQL database with INTERVAL 1 DAY. But here is my question, how would I calculate the number of day intervals if the months change?
Everything would work great if the user stays within one month so it would be something like [March][20][2012] - [March][29][2012], and I can easily calculate the value is 9, but when it's something like [February][27][2012] - [March][20][2012], how can I calculate the number of days in between?
Just to clarify any questions that may arise, I'm using PHP and MySQL and would prefer to stay within those bounds.
The MySQL DATEDIFF function should accomplish the task
DATEDIFF
Dates are not scalars and should not be treated as such. My advice is to user the proper tools for date arithmetic.
A lot of people suggest unix timestamp oriented date math:
$a = '2012-02-12 14:03:50';
$b = '2012-05-30 00:55:03';
$days = (strtotime($b) - strtotime($a))/86400;
However, daylight saving time and all of kinds of factors can make that type of math wrong.
My approach is to typically use DateTime:
$a = new DateTime('2012-02-12 14:03:50');
$b = new DateTime('2012-05-30 00:55:03');
$diff = $b->diff($a);
//$diff is now a DateInterval
However, to answer your real question, I would not pull the data from MySQL based on MySQL date math, but rather I would just give it dates.
WHERE d >= '2012-02-27' AND d <= '2012-03-29';
Though based on your requirements, you may need to alter the 27 to 26 as to grab the previous day and do the calculations with it.
As for doing the changes in point values, I would either precalculate and store them, or I would just calculate them in PHP. There's no simple way to tell SQL "hey grab every record between these dates and while you're at it, do some math with each record's previous record."
I hope this has been clear, but I have a feeling it borders on rambling other than clarity, so if so, please let me know and I'll edit my answer.
I have a table with 2 rows, one of which represents date and the other time. These rows are not date format; they are int type and I can't change the original rows' type. The date entries are written as 20120306, and the time entries are written, for example, 13000 for 01:30 UTC in 5 digits, and 130000 for 13:00 UTC in 6. What I need to do is put both strings (date and time) into 1 UNIX timestamp. I can either use this in a php script or merge them into a new table, whichever works best. The problem for me is the php mysql syntax, functions and sequence for putting these 2 odd strings together into one timestamp.
If I can figure out how to do this, it would help me solve a whole mess of inaccuracies in a calendar reminder script I am trying to put together. I have tried configuring these strings into times separately in the queries, but no matter how it's filtered, the outcome is right in some circumstances and wrong in others due to the nature of the original program. If I had a PHD in PHP I would rewrite the entire program but I don't. I am a major newbie at this. So I just need to write my little PHP scripts to utilise what has already been provided. Any help in my learning quest would be appreciated.
The function strtotime() can also read compound formats.
If you take a look at that list you will notice that your format is a close match to the "XMLRPC (Compact)" listed there.
It's your date string concatenated with a "t" and the time string. So, after you expanded your time string to length 6 (you already know how to do that), you can produce such a string and strtotime() will output your Unix time stamp:
$dt = "20120306";
$tm = "130000";
$unixTime = strtotime($dt . "t" . $tm);
So I've got a simple query in MySQL that sets a new member's expiration date once they pay their dues:
UPDATE members SET joined=CURDATE(), expires=DATE_ADD(CURDATE(), INTERVAL 1 YEAR), active='1' WHERE id=1000
this query has run 200+ times, normally with the correct result - the current date is put in the joined field, and a year from that date in the expires field. However, in about 10 instances, the expires date has been set to 00-00-0000 with no obvious explanation. I started writing the query to a text file every time to make sure the syntax was correct and I hadn't missed anything - and I didn't - it's exactly that query (with only the ID varying) for every query, those that work, and those that don't.
The only thing I can think here is that there must be an issue with MySQL's DATE_ADD function. Has anyone else experienced anything like this?
UPDATE:
I should add that the joined field is correct with the current date in the cases where the expires date is incorrect.
I'm using MySQL 5.0.81.
There are no triggers.
The table is using MyISAM.
IMPORTANT UPDATE:
I'm an idiot - when I say 11-30-1999 that's not actually what's in the database. I absent-mindedly wrote that, but in fact the database contains the value 00-00-0000 - 11-30-1999 is just how it gets rendered by PHP onto my page. Sorry about that, hopefully that will make this problem less difficult to figure out.
Just a thought.... those "wrong" dates didn't happen to be leap year dates did they.... Feb. 29th for example?
It shouldn't matter, but it may be a bug.