I have a database (mySQL) with a time field. I want to write a simple line or two in my php script to add 12 hours to the time IF the time is less than 12:00:00
Please can anyone help (newbie!)
Kind regards.
Philip.
You should use MySQL for that because it is faster and less to write :-)
This doesn't update the column but fetches the time according to your expectation.
Make sure to actually have time column as TIME/DATETIME type.
SELECT
IF(HOUR(timecol) > 12,
DATE_ADD(INTERVAL 12 HOUR, timecol),
timecol
) AS new_timecol
FROM table
Also keep in mind:
1st of month, 23:30 o clock.
+ 12 hours =
2nd of month, 11:30 o clock.
If you want to select the time in 24 hour format, there are flags which simply allow you to do so without adding 12 hours or something.
all you need to do is check if it less than 12 then add an interval of 12 hours or else just put in the time like so :)
SELECT
CASE
WHEN timecol < '12:00:00' THEN ADDTIME(timecol, '12:00:00')
WHEN timecol >= '12:00:00' THEN timecol
END
FROM test
DEMO
EDIT:
with the newly provided information you should incorporate it into the original select like so.
SELECT ff_id, book_cancel, booking_date,
pax_number, room, name, tel_number,
email, voucher_type, voucher_number, notes,
CASE
WHEN time < '12:00:00' THEN ADDTIME(time, '12:00:00')
WHEN time >= '12:00:00' THEN time
END AS time
FROM tables
WHERE booking_date>= '" . date("Y-m-d") . "'
ORDER BY booking_date ASC, time ASC
Related
I have a MySQL table with the following columns:
id, month, day, remind_days_before
It's for a simple recurring reminder function. The user can enter for example: "I have a very important thing on the 5. April every year, please remind be 15 days before that". There could be tens of thousands of entries in this table. Using this table I want to run a cron_job every morning, which is sending out this reminders in form of email messages. The problem is I don't know how to write this kind of SQL query... Is it even possible? I want to query only those rows where the "month" and "day" as DATE is between TODAY and TODAY+31 days (this is the maximum number for remind_days_before).
I'm trying with this right now, but the $end_month is giving me the same as the $today_month:
$today_month = date('m');
$end_month = date('m', strtotime('+31 days', $today_month));
What happens when the +31 days DATE is in the next YEAR or we have leap year?!
Can someone help me out here?
Thank you very much.
First of all, your fields must be a no-mysql function.
So replace month/day with planning_month/planning_day, because the month and day words, are the functions of MySQL, and you may have errors if they are not quoted
Here's a working query. I've tested and works great:
select * from MyTable
/*CHECK CURRENT YEAR*/
where date(concat(year(now()), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
OR
/*CHECK NEXT YEAR*/
date(concat(year(date_add(now(), INTERVAL 1 YEAR)), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
Live Example
Your, it is a very useful question for me too
Just check for both years:
select *
from your_table
cross join (select year(now()) as current_year, year(now())+1 as next_year) vars
where curdate() + interval remind_days_before day = concat(current_year,'-',month,'-',day)
or curdate() + interval remind_days_before day = concat( next_year,'-',month,'-',day)
Need help here, having an mysql table called APPROVAL, there having an id,dateandtime and level, i need a query that selects the id alone with the following condition.
Taking date alone from database and comparing it with current system date, if the days exceeds above 30 and below 60 and also level = 5.
How can I write a query for this.
Thanks in advance.
MySQL has good date arithmetic. For example, the expression
CURDATE() + INTERVAL 30 DAY
gives a datetime value denoting midnight 30 days hence. Similarly
CURDATE() + INTERVAL 61 DAY
yields midnight on the 61st day.
So a query of the form
SELECT ID
FROM APPROVAL
WHERE Level = 5
AND `DateTime` >= CURDATE() + INTERVAL 30 DAY
AND `DateTime` < CURDATE() + INTERVAL 61 DAY
will yield what you want. Notice the use of >= for the beginning of the range of days, and the use of < and an extra day for the end of the range. We do that because we want all items from the 60th day, and none from the 61st day.
A compound index on (Level, DateTime) will make this query very efficient to satisfy.
Notice that an expression like
DATE(`DateTime`) <= CURDATE() + INTERVAL 60 DAY /* slow! */
will also yield correct results, but the presence of the the DATE() function call on the column to be searched makes it unsargeable. That is, it makes MySQL unable to use an index to satisfy the search.
Ok so use this query to retrieve all the IDs that match level 5 and date diff between 30 and 60 compared to the current date.
SELECT id
FROM APPROVAL
WHERE level = 5 && DATEDIFF(NOW(), dateandtime) BETWEEN 30 AND 60
I'd suggest you to order them dy date DESC too.
Hope that helps
I hope, I understood your problem correctly.
select `ID`
from APPROVAL
where `Level` = 5
and ( DATE(`DateTime`) > curdate() + interval 30 day
and DATE(`DateTime`) < curdate() + interval 60 day )
order by `ID` asc;
Where DATE() gets the date from a datetime and CURDATE() is the current system date. With interval you can manipulate a date expression whitout having to worry about its limits.
Im using this to calc total hours of work:
$result = mysql_query("CREATE OR REPLACE VIEW ff AS SELECT vecka, namn, dagid, start, stop, vstart, vslut, arbtid, arbtim, UNIX_TIMESTAMP( stop )/3600 - UNIX_TIMESTAMP( start )/3600 AS value2 FROM schematabell;");
Now i want to calculate the time between the column "start" and the time "19:00" the same day, (if column "stop"is greater then "19:00"). I also want to calculate the time between "19:00" and "23:59" (if stop is greater then midnight). How do you achieve this. The start and the stop column is in datetime format.
You can find the difference in SQL. The following statement should help. I currently cannot check if it works in real database.
TIMEDIFF(start,DATE_SUB(CURDATE(), INTERVAL 19 HOUR))
I am working on a project where one "lights a virtual candle" and I want to create a cron job that selects all records from the database that are expiring in the next five days, possibly calculated from the databases "created_date" field which type is "Type: TIMESTAMP > CURRENT_TIMESTAMP"
Process:
The candle duration = 30 days
Alert period = 5 days before the 30 days
This is what I have so far (I can do the rest, it is the query I am having problems with)
$query_rsQueryA = "SELECT * FROM $databaseName WHERE created_date + INTERVAL 5 DAY < CURRENT_TIMESTAMP;";
$rsQueryA = mysql_query($query_rsQueryA) or die(mysql_error());
$row_rsQueryA = mysql_fetch_assoc($rsQueryA);
$totalRows_rsQueryA = mysql_num_rows($rsQueryA);
Thanks in advance!
It appears what I have written above actually works, should have tried it first guys, sorry about that!
You can use the following
datediff(now(),date_add(created_date,INTERVAL 25 day)) > 0
So basically its adding 25 day to the created_date and then finding the difference with the current date and if its greater than 0 meaning start sending alert
I need to find the birth day of people from the table .. coming in next 7 days from today.
I have a query ..SELECT * FROMtableWHEREdobLIKE BETWEEN %-08-17 AND %-08-24 but it returns the records whose dates are not submitted in database..i mean the entry is 0000-00-00
I have stored the birthdates in dates format in table. Please Help me finding the bug.
Since this is mysql, I don't know if DATE_FORMAT() can work on this. But give this a try.
SELECT * FROM users WHERE DATE_FORMAT(dob, '%c-%d')
BETWEEN DATE_FORMAT('1983-08-17', '%c-%d')
AND DATE_FORMAT('1983-08-24', '%c-%d') OR (MONTH('1983-08-17') > MONTH('1983-08-24')
AND (MONTH(dob) >= MONTH('1983-08-17')
OR MONTH(dob) <= MONTH('1983-08-24')))
any year can be used (just to complete the date format) since year does not matter
UPDATE 1
Tested it on SQLFiddle.com
SQLFiddle Demo
UPDATE 2
I'm sorry for my first answer. I honestly missed to read this line coming in next 7 days from today. And I think that was the reason why I was downvoted by Imre L. He has his point. The reason why I posted the answer like that was because I thought the OP was asking for the days in between regardless of the year. So here is the update.
SELECT ....
FROM ....
WHERE DATE(dob) BETWEEN NOW() AND NOW() + INTERVAL 7 DAY
Hope it's clear now. :D
this will handle correctly cases wen there is a month or year change between the date range:
select *
from people
where (DAYOFYEAR(dob)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(dob),1000,0))
between DAYOFYEAR(CURDATE())
and (DAYOFYEAR(CURDATE() + INTERVAL 7 DAY)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(CURDATE() + INTERVAL 7 DAY),1000,0))
By converting the dob date into this year's date you can avoid issues where the period crosses a month or year boundary. This selects all rows where the birthdate occurs in the coming week:
SELECT * FROM users
WHERE concat( year(now()), mid(dob,5,6) )
BETWEEN now() AND date_add(now(), interval 7 day)
SELECT
str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d') BIRTHDAY,A.*
FROM app_membership A
WHERE str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d')
BETWEEN str_to_date('15-10-2017','%d-%m-%Y') and str_to_date('10-11-2017','%d-%m-%Y')
ORDER BY BIRTHDAY ASC;
Try this. Worked for me.
Lets list people who born in any month/year between december 14 and august 24. We know its an year before another one. We must count with months in the previous year. It's complex because you may have a problem comparing the month of the starting date with the month of the ending date. However, it may be solved with this query:
SELECT * FROM t_users WHERE (DATE_FORMAT(d_birth, '%m-%d')
BETWEEN DATE_FORMAT('2017-12-14', '%m-%d') AND
DATE_FORMAT('2018-08-24', '%m-%d'))
OR(MONTH('2017-12-31') >= MONTH('2018-08-24')
AND (MONTH(d_birth) >= MONTH('2017-12-31')
OR MONTH(d_birth) <= MONTH('2018-08-24')))