generate timezone specific date and time - mysql

My mysql query is as follows:
select * from orders
where orders has created_at field.
Now what i want is i want to convert created_at to 7 am of next day of created_at date . and the time created should be according to local timezone.
Is there a way to achieve it?
Note:: and orders table also has field timezone which has values like Australia/Sydney , Asia/Kolkata etc.

You need to go through the CONVERT_TZ function
// syntax
CONVERT_TZ(your_timestamp_column_name, 'db_timezone', 'your_desired_timezone_name')
// Example
SELECT CONVERT_TZ(`created_at`, 'UTC', `timezone`) as `my_date` FROM orders
Now when you have a date in your desired timezone, you can add hour and time that comes from a difference of next day 7AM and .my_date

Related

How to get rows after a time of day using datetime column

I am running queries for a ticketing system. I want to extract all tickets created after 6:00PM or 18:00:00 in database/military time.
Could I use a DATEPART function or EXTRACT function?
Something like this may work:
SELECT *
FROM ticket
WHERE TIME IS AFTER 6PM
The issue is that the datetime format is as '2014-12-01 16:13:38' so I would need to specify for only the characters after the DATE section.
In MySQL, just use the hour() function:
SELECT *
FROM ticket
WHERE hour(time) >= 18;

take today's values between given hours in defined date-time format

I have to make a SELECT from a table where the field with the time is in format: y-m-d h-m-s but I have to output the values that are entered for the current day between 00:00:01 and 23:59:59. My query looks like that:
SELECT user, insert_time FROM t_users
WHERE insert_time BETWEEN '2014-06-30 00-00-01' AND '2014-06-30 23-59-59'
The problem is that I don't have to hardcode the date... only the hours interval. And to make it better to understand the current problem I will make a wrong input what is needed to be done: 'TODAY 00-00-01' AND 'TODAY 23-59-59' which of course don't work but if there is a way to make a query that will output the today's added values, I will be grateful.
You can do this kind of calculation with PHP:
http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
or MysQL solution:
http://blog.ubiq.co/difference-between-two-dates/
Also for today entries I think you can use:
SELECTid FROM my_table
WHERE
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),'%Y%m%d000000')
AND
timestamp >= date_format(CURRENT_TIMESTAMP(),'%Y%m%d000000')

How to match current date with the database entries and retrieve it

Suppose I have a MySQL table called birthday with dob as a column. dob has lot of dates in date time format. I have a perl variable $test=current date. My problem here is, how can I get the all the values present in the dob column that matches with the day and month of $test.
Let us assume, $test='2013-05-28 00:00:00' So, now I dont know how to write a query to select all the entries in MySQL table which matches the day and month of the $test i.e 28 and 5 respectively.
You need to use Month and Day function inside mysql query like
select * from birthday where Day(dob) = Day('2013-05-28 00:00:00')
and Month(dob) = Month('2013-05-28 00:00:00');
Hope it works.

SQL extra only the day instead of entire date

I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers
Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table
you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );

select rows based on todays date

I am trying to select records from a database only if they match today's date. The format for the date in the database is 2012-06-20 9:30:00 I am using the statement SELECT id FROMnewsreportsWHERE DATE(newsdate) = CURDATE() but it doesn't not return any records for today?
Screenshot of column with dates
http://img135.imageshack.us/img135/8399/2347f03df0394cd898c7fc5.png
DATE_FORMAT(NOW(),'%Y-%m-%d') = FORMAT_DATE(NOW(newsdate), '%Y-%m-%d')
Or better:
DATE(newsdate) = DATE(NOW())
The best way is to store the additional column with 2001-09-11 date format and compare this one
Example, thanks to #Conrad Frix
It looks like the curdate function is going to give you something different than the date format you've got in the database.
Take a look at the docs here
MySQL Date and Time Functions
It's supposed to return a date in this format:
2008-11-11
So, you could either search for a date range between curdate() and curdate() + INTERVAL 1 DAY (untested), or store the dates in the curdate() format.