MySQL yearweek function space - php

I'm using the function yearweek in a select query.
My question = when the date = 2017-02-13 || yearweek = 201707. How can I get a space between 2017 07

You could use DATE_FORMAT:
CREATE TABLE tu(datum DATE);
INSERT INTO tu(datum) VALUES ('2017-02-13');
SELECT datum, DATE_FORMAT(datum, '%X %V'), YEARWEEK(datum)
FROM tu;
Output:
13.02.2017 00:00:00 2017 07 201707
Rextester Demo
%X Year for the week where Sunday is the first day of the week. Used with %V
%x Year for the week where Monday is the first day of the week. Used with %V

Related

select varchar as date in mySQL

I have a date column in the format
25 Mar 2017 07:19 pm
I want to select date in the format dd/mm/yyyy
I have tried: cast(date as DATE), STR_TO_DATE(date, '%d/%m/%Y') but everything returns null
any help will be appreciated.
Use
select str_to_date('25 Mar 2017 07:19 pm', '%d %M %Y')
The format specified has to match the input pattern. As the month name is included, use %M.
You can parse the date using following format:
SELECT STR_TO_DATE('25 Mar 2017 07:19 pm', '%d %M %Y %h:%i %p');
Here's the SQL Fiddle.

PHP Count Between Date and Todays Date

In my dates database I have a table of dates that have two followups to be completed, one after 30 days, one after 60 days. I need to build a page that uses the MySQL query to pull all dates from the dates table that have a 30day value of No (which I can do). Now the tricky part is, I need it to only output the dates that meet that criteria, and are 30 days from the current date.
For example: August 4 & 6 have a 30day value of No, August 5 has a 30day value of Yes. Today's date is September 4. 30-days prior would be August 5.
I need the query to only display August 4 in this case, since it hasn't been 30 days since August 6 and August 5 has already been done.
I am unsure what kind of function to use to do this counting. I appreciate your help
EDIT:
Date - 30day Value
July 1 - Yes
July 5 - No
August 1 - No
August 5 - No
August 6 - Yes
Today's Date is September 2.
The table would display July 5 and August 1, as their 30day values are No, and they are more than 30 days from todays date.
You should use DATEDIFF function:
SELECT ....
FROM your_table
WHERE DATEDIFF(CURDATE(), event_date) = 30
Where event_date is example of your date column.
MySQL's DATEDIFF function allows you to subtract 2 dates in a query.
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
For example:
SELECT some_id, date_column
FROM date_table
WHERE DATEDIFF(CURDATE(), date_column) = 30
You could also select both 30 and 60 days like this and also have a cutoff date of 60 days so it's not searching the whole table:
SELECT some_id, date_column
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
AND DATEDIFF(CURDATE(), date_column) IN (30, 60)
And since I'm making some assumptions with my understanding of what you're asking, you may also want to do this which will return the results as 'Yes' or 'No' in your result set:
SELECT some_id, date_column,
CASE DATEDIFF(CURDATE(), date_column)
WHEN 60 THEN 'Yes'
WHEN 30 THEN 'Yes'
ELSE 'No'
END CASE AS is_3060_day
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
Alternatively if you want to accomplish this on the PHP side, you could use PHP's date_diff function:
http://php.net/manual/en/function.date-diff.php
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
$result = dateDifference($date1, $date2)
if ($result==30 || $result==60) {
// Do something
}
you can fetch both the dates and use the php function
$prevdate = date_create("2013-03-15");
$currdate = date_create("2013-12-12");
$diff = date_diff($prevdate,$currdate);
echo $diff->format("%R%a days");
Output
272 days

Get last 24 hours data from SQL

Hi all i want to select from my table all records from last 24h , 7days , 14 days ....( my sql timespan format is date("l, M d Y, h:i:s A")) so when i try to receive the data from last 24h is not working for me any help plz
exemple
if i get all date from
$result = mysql_query("SELECT * FROM stats");
result is Day,Date,time Count
Saturday, Feb 27 2016, 02:28:59 PM 27191 Saturday, Feb 27 2016, 03:28:05 PM 28659 Saturday, Feb 27 2016, 04:27:26 PM 30138
so i try like that and not working any help plz
$result = mysql_query("SELECT * FROM stats WHERE timespan >= now() - INTERVAL 1 DAY");
SELECT * FROM stats WHERE STR_TO_DATE(timespan,"%W, %b %d %Y, %h:%i:%s %p") >= date_sub(now(), INTERVAL 1 DAY)
STR_TO_DATE will convert your the string into date format (Y-m-d H:i:s) and then you can compare it with the date returned by now()
This should work. Make sure "timespan" exists and that it is a date. According to your example you should test on the "date" field.

how can i get month and year from custom formated datetime in mysql?

I want to get month and year from this datetime format in form of number of month.
My datetime formate is 19 August 2015 - 10:50 am.
How can I get this?
I suppose you have a MySQL Varchar field that contains a custom string like '19 August 2015 - 10:50 am' that represents a date, you can get the date value with this:
SELECT str_to_date(customdate, '%c %M %Y - %h:%i %p') as date_column
FROM yourtable
Please see the str_to_date function documentation, and the list of available date formats.
Then you can just use MONTH and YEAR functions:
SELECT
YEAR(str_to_date(customdate, '%c %M %Y - %h:%i %p')) AS year,
MONTH(str_to_date(customdate, '%c %M %Y - %h:%i %p')) AS month
FROM
tablename
You can use date() and strtotime().
Will there always be a hyphen (-) in your date? Then, we have to remove it first by using str_replace().
$date = "19 August 2015 - 10:50 am"; /* YOUR GIVEN EXAMPLE */
$date = str_replace("-", "", $date); /* REMOVE HYPHEN */
$date = date("m Y", strtotime($date)); /* 08 2015 */
Us this
<?php
//date_default_timezone_get('Asia/Colombo');
$date = '19 August 2015 - 10:50 am';
echo date("Y/m", strtotime($date)); //2015/08
echo date("Y-m", strtotime($date)); // 2015-08
?>

INTERVAL 30 DAY not giving expected result

I am somewhat familiar with the MySQL DATE and DATE_FORMAT.
However, when running a query for a date range, using INTERVAL - 30 DAY, am either not getting the correct output or it throws an error. Looking further into the table, realize that each row for date_buy is in this type of format:
Mon 09 September 2015, etc. I have tried to use PHP date('D d F Y') instead of DATE(NOW()) and such, but am unable to resolve the issue. Is there another known workaround for this?
And yes, have tried a BETWEEN date_buy_x and date_buy_y. It returns nothing even though there are records in the table.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy >= $date - INTERVAL 30 DAY
GROUP BY date_buy
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy BETWEEN date_buy_x && date_buy_y
GROUP BY date_buy ORDER BY date_buy DESC
UPDATE
SO, based upon the suggestions, and looking further into the DATE_FORMAT and CURDATE parameters, have been able to display the data. The ORDER BY must also be in DATE_FORMAT(date_buy, '%a %d %M %Y') to output correctly. However, the issue still remains regarding INTERVAL 30 DAY parameter.
WHERE datebuy <= DATE_FORMAT(CURDATE() - INTERVAL 30 DAY,'%a %d %M %Y')
GROUP BY date_buy ORDER BY DATE_FORMAT(date_buy, '%a %d %M %Y') DESC
This is bringing back data but still way out of range, which makes me think it is not accepting the INTERVAL request.
Sat 22 November 2014 (50 sales)
Thu 18 December 2014 (50 sales)
Thu 22 January 2015 (20 sales)
Sun 25 January 2015 (20 sales)
Mon 06 April 2015 (25 sales)
Sun 12 April 2015 (25 sales)
Mon 03 August 2015 (10 sales)
When the output should only be:
Mon 03 August 2015 (10 sales)
You could conceivably do:
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= $date - INTERVAL 30 DAY
GROUP BY date_buy
Unless $date is also in this string format, in which case you need to use the STR_TO_DATE function on it too.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= DATE(STR_TO_DATE($date, '%a %d %M %Y')) - INTERVAL 30 DAY
GROUP BY date_buy
how about date_sub(),use date_format to same format when query
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE_FORMAT(DATE(date_buy,'%Y %m %d')) >= DATE_FORMAT(DATE_SUB(DATE($date,INTERVAL 30 DAY),'%Y %m %d'))
GROUP BY date_buy

Categories