How to get exact date from 30 days from specific date? [duplicate] - php

This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Add number of days to a date
(20 answers)
Closed 9 years ago.
I have mysql date like this 2013-10-16 17:44:13 and I need to get +30 days from there. I can't use eg. 10th ( month ) and change it to 11th ( month ) as this may be 31 day or 29 depending on the month
I can only think of converting 2013-10-16 17:44:13 to timestamp than + 30*24*60*30, and than this new timestamp back to mysql format
Is there a better way?

You can use strtotime for this:
$date = date('Y-m-d H:i:s', strtotime($date . ' +30 days'));
or do it directly in MySQL using DATE_ADD:
SELECT DATE_ADD(`date`, INTERVAL 30 DAY) as `date` FROM `table`
If you run a newer version of MySQL, you don't need to use DATE_ADD:
SELECT (`date` + INTERVAL 30 DAY) as `date` FROM `table`
Please note that while strtotime is smart enough, MySQL requires you to use DAY. Not DAYS.
Edit: I am unable to find any proof of DATE_ADD being needed in older versions, but I swear that I've heard it somewhere. Take it with a grain of salt and use whatever method you prefer.

since you mentioned mysql you an do it with mysql functions
select NOW() + interval 30 day as NEW_DATE
NOW, could be replaced with a date in your db
select date_field + interval 30 day as NEW_DATE from YOUR_DB

Related

Check if datetime is older than 5 days [duplicate]

This question already has answers here:
SELECT all records that are 30 days old
(4 answers)
Closed 1 year ago.
So I am going to make a cron job in cPanel that runs every day, but not sure how the query should look like. I have a datetime column with this type of value: 2021-04-06 14:12:06. How can select from table where datetime column is older than 5 days?
You can use date arithmetic. Assuming you mean "day" without the current time value:
where datetime_col < curdate() - interval 5 day
If you want the reference to be the current time rather than the current date:
where datetime_col < now() - interval 5 day

MySQL Query - Records between 15 days ago End_date to till End_date

I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO

PHP find date after six months added to now() function [duplicate]

This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Closed 8 years ago.
I have a subscription system in my website. When the users subscribe, their start date and time is added using the now() function. I also need to calculate the date and time six months from now and store it in database. How can I do it?
DATE(DATE_ADD(NOW(), INTERVAL 6 MONTH))
can be used directly in your query and will do the job:
INSERT INTO table (datenow,dateplus6m) VALUES (NOW(), DATE(DATE_ADD(NOW(), INTERVAL 6 MONTH)));
Explanation from the docs:
DATE_ADD(date,INTERVAL expr unit)
These functions perform date arithmetic. The date argument specifies the starting date or datetime value. expr is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string; it may start with a “-” for negative intervals. unit is a keyword indicating the units in which the expression should be interpreted.
The date_add function is what you're looking for:
INSERT INTO users
(username, registration_date, six_months_date)
VALUES ('someone', NOW(), DATE_ADD (NOW(), INTERVAL 6 MONTH));
Try this
$newDate = date('Y-m-d', strtotime("+6 months", time()));
echo $newDate;
OUTPUT
2015-06-01
You can use date function for adding months in php .
Suppose $date contains current date,$date1 contains current date + 6 months
Here is the code
$date=date("Y-m-d");
echo $date;
$date1=date('Y-m-d',strtotime("6 months"));
echo $date1;
Here is the manual for other options
Hope this helps!

Need all MySQL rows with dates in the next 7 days, but the date column is not formatted normally

I have a column in my database 'scheduledDate', I need all rows in the database that have a scheduledDate within 7 days from today.
I could use a query like this:
SELECT * FROM tblName WHERE `scheduledDate` > DATE_ADD(now(), INTERVAL 7 DAY
The only problem is, the 'scheduledDate' column is not formatted as a mysql timestamp (YYYY-MM-DD HH:MM:SS), but is formatted just as a standard American Date (07/09/2014). Is there a way to grab all rows within the next 7 days in my query? I was thinking that it might be possible using DATE_FORMAT, but I have been unable to figure it out.
You can convert the string to a date using str_to_date():
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN now() and DATE_ADD(now(), INTERVAL 7 DAY)
The logic also needs to change. The above uses between, but this may not be the logic your really need because of the extraneous time component on now(). Perhaps this is closer:
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN CURRENT_DATE() and DATE_ADD(CURRENT_DATE, INTERVAL 7 DAY)
WHERE scheduledDate >= Date_Add(now(), INTERVAL -7 DAY)

Getting mysql result from the last 30 days [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get from database but only last 30 days
Hi I have some php code which I use to count the rows in a database from the last 30 days. The problem is, that if I change the piece of code so that the number changes from -30 to -20, the output number goes from 272 to 360 instead of going down.
Here is the code:
$result = mysql_query("SELECT * FROM all_count WHERE DATEDIFF(date,NOW()) = -30 and member ='000002'");
$num_rows60 = mysql_num_rows($result);
Try this
select * from `table` where `yourfield` >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
For days, year see below for example.
DATE_SUB(CURDATE(), INTERVAL 15 DAY) /*For getting record specific days*/
DATE_SUB(CURDATE(), INTERVAL 1 YEAR) /*for getting records specific years*/
For Anand, query
BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE() ,INTERVAL 3 MONTH )
/* For Getting records between last 6 month to last 3 month
It's better to compare
`date`< DATE(NOW() - INTERVAL 30 DAY)
rather than
DATEDIFF(date,NOW()) = -30
In the first case, the date calculation is done only once, at the beginning of the query, and the database can use any indexes on the date column.
The second query must calculate the DATEDIFF on every row, and the database can't use any indexes. The second query forces a full table scan.
Also, I strongly suggest that you not call your column date. Yes, I know you can quote the name with backticks, but that's just messy and when you forget then your syntax errors will be hard to forget. Come up with a more descriptive name for the column. What kind of date is it? What does the date represent?
You can use this instead:
$result = mysql_query("SELECT * FROM all_count WHERE `date`< DATE(NOW() - INTERVAL 30 DAY) and member ='000002'");
As you can see in the documentation here, the DATEDIFF function in MySQL tells you the difference in days of the first date to the second.
Your query only selects all rows where the difference is exactly 30 days and not those that are up to 30 days ago. So it's completely possible, that the number of rows for the date 20 days ago is higher than 30 days ago. What you most likely wanted was:
SELECT * FROM all_count WHERE DATEDIFF(date,NOW()) >= -30 and member ='000002'

Categories