I have this certain problem about mysql date functions.
I'm trying to compare the value of THIS MONTH to the given timestamp in database.
For example, month today is june, and the timestamp is 1369967316
And I'm trying to determine if that timestamp is in month of june.
$query = db_query("SELECT * FROM users WHERE MONTH(FROM_UNIXTIME(CURDATE())) = MONTH(1369967316)");
//count total members this mont
$members_month = $query->rowCount();
so if I used the rowCount, the $members_month should have the value of 1.
Unfortunately it doesn't work.
Any help would be appreciated.
Well I saw some answers that some kind of relevant to mine but it doesn't hit the spot or I didn't applied it well.
mysql get month from timestamp not working
how to use curdate() in where clause against unixtimestamp (bigint) column
This works for me:
mysql> SELECT MONTH(FROM_UNIXTIME(1369967316));
+----------------------------------+
| MONTH(FROM_UNIXTIME(1369967316)) |
+----------------------------------+
| 5 |
+----------------------------------+
Your issue is likely coming from the fact that 1369967316 is May 30th, not June (as you expect), thus resulting in an inequality with MONTH(CURDATE()).
mysql> SELECT FROM_UNIXTIME(1369967316);
+---------------------------+
| FROM_UNIXTIME(1369967316) |
+---------------------------+
| 2013-05-30 22:28:36 |
+---------------------------+
Related
This SQL is called by a Smarty File designed to download an .xlsx of the output. It works well for all months EXCEPT the Dec. (end of year) report. Something is needed to get it to cross the year-end boundary but I can't figure what. ? Thanks.
SELECT user_id, cert_name, from_email, created_at, phone as role,
delivery, user_street, user_city, user_state, user_zip,
user_country_id, standard_fee, expedited_fee
FROM support_tickets
WHERE nature = 2
AND user_id IS NOT NULL
AND created_at BETWEEN '2012-12-05 04:00:01' and date(concat_ws('-', #year, #month + 1, 1))
ORDER BY created_at DESC
Use date arithmetic, not string concatenation.
If you try to add #month+1 when month is 12, you get month 13, not month 1 of the next year. There is no month 13 in calendars currently in use.
For your case I would recommend using the LAST_DAY() function that is built into MySQL. This returns the last date in the current month of its argument.
Example:
mysql> SELECT LAST_DAY('2020-12-05') AS last;
+------------+
| last |
+------------+
| 2020-12-31 |
+------------+
I suspect you want the last day of the same month.
If you really want the first day of the next month, use date arithmetic:
mysql> SELECT LAST_DAY('2020-12-05') + INTERVAL 1 DAY AS next;
+------------+
| next |
+------------+
| 2021-01-01 |
+------------+
I need a solution for my project..Actually, I got trapped in a problem..Let me show you the picture of my problem.
Senerio of my problem
If you can see, there are some columns of 12 months.So I need to run a MySQL query or a program which can show me the selected columns which contain the text "Not Paid".
For example.
If I want to check the members between January to March or June to November etc. who are "Not Paid"..so what is the solution for that...??
I know that we can use the "between" clause but I am not getting the idea..Because I want to use two drops down menu where the user can select from which month to which month they want to see.
Please help me to get rid out of this problem..I am a new beginner in PHP.
If I were you I would represent the months as dates, even if strings (e.g., '2017-06') and then map the month to the semantic representation string (e.g., 'June'). So one way is to store the date in your MySQL database as a varchar.
In terms of modeling the data in your database, I would create a table with three columns 1) the user_id; 2) the due_date; 3) the payment_status.
.----------------------------.
| 0 | 2017-06 | paid |
| 1 | 2017-07 | not paid |
| 2 | 2017-08 | paid |
| 3 | 2017-09 | paid |
'----------------------------'
Now you can write a query to retrieve the members between January to March who has "Not Paid". (note: this is just one way, there are many ways to query this)
SELECT
DISTINCT(user_id)
FROM payments_table
WHERE due_date >= '2017-03'
AND due_date < '2017-06'
AND payment_status = 'not paid';
In my MySql DB I have these fields:
id | email_id | interval | start_at | last_sent_at
--- | -------- | ---------- | ------------------- | -------------------
1 | 8293 | +6 months | 2017-06-14 16:59:54 | 2017-06-14 16:59:54
--- | -------- | ---------- | ------------------- | -------------------
2 | 8904 | (NULL) | 2017-05-14 12:32:45 | (NULL)
I am trying to create a dynamic way for users to set a schedule for an email job in laravel. The idea is to use laravel's commands scheduler to run a command that checks all scheduled emails and then runs them if they haven't been sent (whereNull(last_sent_at)) or if the last time they were sent was more than the current time minus 6 months or 12 months or what ever they select for that field.
I have tried several different queries to get this to work to no avail. In laravelized code I have tried the following...
$schedules = Schedule::selectRaw('schedules.schedulable_id, schedules.schedulable_type, UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(schedules.interval) as NowPlusInterval')
->whereNull('last_sent_at')
->orWhere('schedules.last_sent_at', '>=', 'NowPlusInterval')
->get();
and
$schedules = Schedule::whereNull('last_sent_at')
->orWhereRaw('schedules.last_sent_at >= NOW()-'.Carbon::parse('schedules.interval')->toDateTimeString())
->get();
and many many other variations. The second one is the simplest to me but because I am passing the db field to a php function it doesn't recognize that I am trying to get the value from the db field (not parse a string called 'schedules.interval'). Is there anyway write this field into a variable that Carbon can parse or is there a raw mysql query that I can run that will give me all the fields where last_sent_at is null and where now minus the interval listed is more than the last_sent_at.
I've racked my brain on this for hours. Any help is appreciated.
Try using mysql's DATE_SUB() and INTERVAL functions. Something to the effect of:
->whereRaw('last_sent_at >= SUB_DATE(NOW(), INTERVAL 6 MONTH)')
or perhaps this:
->whereNull('last_sent_at')
->orWhere(DB::raw('DATE(`schedules.last_sent_at`) >= DATE_SUB(NOW(), INTERVAL `schedules.interval`')))
->get();
i have table:
Birthday
id | name | date
1 | aaa | 1990-03-02
2 | bbb | 1990-03-12
3 | ccc | 1990-03-25
4 | ddd | 1990-04-25
5 | eee | 1990-04-23
6 | fff | 1990-04-26
7 | ggg | 1990-04-12
How is the best way to SELECT all names where date is 1990-04-xx?
SELECT name FROM table WHERE date LIKE '1990-04%'
Some other answers here are assuming you're storing the date in a datefield, but I assumed by the way it was laid out in your question that it was just a string. Going on that assumption, I knew that using the LIKE operator would let me use a wildcard (the % sign) to search for anything with that year and month. That said, this query will match anything that starts with 1990-04 so there is a possibility with malformed data that you could get some incorrect data (e.g. a date is entered into the database like '1990-041-12')
If it's a DATE value I'd suggest checking if it's between the beginning and the end of the month. If you chop up the date using DATE_FORMAT or some other function you'll lose any chance of optimization:
SELECT name FROM myTable WHERE `date` BETWEEN '1990-04-01' AND '1990-04-30'
If it's a DATETIME value, do this instead to account for values like 4/30/1990 at 9PM:
SELECT name FROM myTable WHERE `date` >= '1990-04-01' AND `date` < '1990-05-01'
If date is a DATE or DATETIME or TIMESTAMP column and you want an index to have chances to be used:
SELECT name
FROM tableX
WHERE date >= '1990-04-01'
AND date < '1990-05-01' ;
An index on (date) would be good. Even better, an index on (date, name).
I have a table with a kind of weird date format in a column as varchar - this is the format that the company has provided me with - the T in the middle seems to mess things up.
EVENTID | EVENT_DATE | EVENT_DURATION
1 | 2012-10-14T06:00 | 15
2 | 2012-10-14T06:15 | 11
3 | 2012-10-14T06:26 | 14
4 | 2012-10-14T06:40 | 10
ect...ect
I have php code to return the current time in the exact same format (with the weird 'T' in the middle'
$thisin = DateTime::createFromFormat('Y-m-d\TH:i', date('Y-m-d\TH:i'));
$thisin->setTimeZone(new DateTimeZone('UTC'));
$thisout= $thisin->format('Y-m-d\TH:i');
Assuming that today is the 14th and the current time is 06:21, how do i query the current row based on duration that matches "$thisout from php" and the next five rows (in the future).
Because the current time and date are returned from php as "2012-10-14T06:21" The query should output
2 | 2012-10-14T06:15 | 11 (Now SHowing)
3 | 2012-10-14T06:26 | 14
4 | 2012-10-14T06:40 | 10
ect ect
I have been scratching my head for hours, DATE_FORMAT() Doesn't seem to work, and I think it may be the T in the middle. I am aslo have to figure out how to use duration to determine if the current time applies to a specific row.
This does not work
SELECT DISTINCT EVENTID, EVENT DATE, EVENT_DURATION
FROM epg_event
WHERE DATE_FORMAT(EVENT_DATE, '%Y-%m-%dT%H:%i') >= DATE_FORMAT(NOW(), '%Y-%m-%dT%H:%i')
ORDER BY EVENT_DATE ASC LIMIT 5
Any Ideas?
You should load the date information into a Date column rather than a varchar column in the database. As previously noted this is the ISO format for dates.
Need to add in the interval.
where event_date + INTERVAL duration MINUTE >= NOW()
I'm not currently able to test it, but something like this might work:
SELECT DISTINCT EVENTID, EVENT_DATE, EVENT_DURATION
FROM epg_event
WHERE (event_date <= NOW()) AND (ADDTIME(event_date, INTERVAL event_duration MINUTE) >= NOW())
ORDER BY event_date ASC LIMIT 5
DATE_FORMAT() is meant for formatting a DATETIME field, not the other way around. And, as mentioned earlier, you event_date should be a DATETIME field and you should convert the time when you import the data to your database.