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!
Related
I am working on a page which contains the following line of mysql. The overal objective is simply to match the current week num to the current date
$sql = "select distinct weekNum from " . DB_PREFIX . "schedule where DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR)"
As per my research DATE_ADD() allows you to add a certain number of days to a date.
Thus I am assuming DATE_ADD(NOW() adds the current date to...well date?
What exactly does INTERVAL do in this statment.
Any help interms of an explanation clarifying above statment will be much appreciated.
DATE_ADD(NOW(), INTERVAL 2 HOURS)
-- This will add 2 hours to the current time
-- The format is DATE_ADD(date, INTERVAL value unit)
value can be anything --> a number
unit will be -- > anything from following list
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH
So you can add minutes , Hours , months into your date using this function
Like that you can use the function DATE_SUB(date, INTERVAL value unit) Which will substract the date hope you are clear. for more information just visit the link [https://www.w3schools.com/sql/func_mysql_date_add.asp]
As far as I can see this statement is trying to manage timezone difference
INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR
should return difference(in hours) for different timezone(server and offset) which is then added to current date to calculate actual time for scheduling.
from https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(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.
It takes a date - is this case "now" and ads some hours to it.
In this case it reads the server timezone offset and adds that.
The result would be a date where you get a selection based on the GMT-0 based time.
The function DATE_ADD() has two parameters:
1. the date to which you want to add any interval
2. the amount of units with key words INTERVAL the numbe UNITS, where 'UNITS' can be any supported time value between MILLISECONDS ~ YEAR
The DATE_ADD() is the synonym of ADDDATE().
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
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 8 years ago.
I have the following SQL query and I would like to output the ageOnLastSeptember date in a UK (dd/mm/yyyy) format. Can anyone tell me how I can achieve this?
$sql="select ( if(month(now()) < 9, year(now()) - 1, year(now())) - year(dob) - if(month(dob)<9, 0, 1) ) as ageOnLastSeptember,first_name,last_name,dob,school,contact_no,family_id,contact_id from members_family where contact_id = '$contact_id' ";
I tried the following but it did nothing
$sql="select date_format(( if(month(now()) < 9, year(now()) - 1, year(now())) - year(dob) - if(month(dob)<9, 0, 1) ), '%d/%m/%Y') as ageOnLastSeptember,first_name,last_name,dob,school,contact_no,family_id,contact_id from members_family where contact_id = '$contact_id' ";
I think it will much easier and better to format the date using PHP rather than formatting using MySQL. After retrieving the data from MySQL you can format using PHP like
$date = date("d/m/Y", strtotime('2002-05-18'));
The output will be : 18/05/2002
So your PHP code will be :
<td>". date("d/m/Y", strtotime($rows['dob'])) ."</td>
More info on PHP date and its format can be found here: http://php.net/manual/en/function.date.php
Hope this helps you :)
You do not need to do formatting at the database level. If you have dates stored as dates (the correct way), just get the records and send it to the client. Formatting should be done at the control which you are using to display these dates.
Not sure how an age can be formatted as a DD/MM/YYYY date. Doesn't seem logical
To get the age in days you could use something like the following
SELECT FLOOR(NOW(), DATE_ADD(dob, INTERVAL (ABS(DATEDIFF(CONCAT(YEAR(DATE_ADD(NOW(), INTERVAL -8 MONTH)),':09:01'), NOW())) * -1) DAY)) AS Age
FROM members_family
WHERE contact_id = '$contact_id'
That could be customised to give an age in years (gets a bit trickier to be 100% accurate with leap years, especially if you want to cope with every 100th year not being a leap year but every 400th year being a leap year)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
What I want to do is to get day from database, current date. And check if difference between them > 1 day:
$curdate= date("Y-m-d H:i:s");
$dbdate is value stored in datetime format in db.
$dif=$curdate-dbdate;
How to check if $dif>1 day ??
Assuming the stored date is expressed in the same time zone as the server, you can convert it to a timestamp using strtotime, and compare it to strtotime("-1 day"):
if (strtotime($dbdate) < strtotime("-1 day"))
frobnicate();
You can get just the day from each date.
$day = intval($curdate= date("d"));
This will get the day as an in. Do the same for the time of the data base and you get two integer representing the day. Using that you can calculate how many days have pass.
Beware that the last line should look like this:
$dif = abs($curdate-$dbdate);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php date compare
I have a date that I take from the mySQL database that looks like this:
2011-06-20
And I get the date of the current day in this way:
$todaydate = date('Y-m-d');
What I need to know is how do I compare the two results?
How can I compare the dates and understand for example if a week is passed from the database date or a month or a year..etc..?
Thank you!!
There is no need to put that burden on PHP when MySQL has built-in functionality for that already. You should take a look at MySQL's DATEDIFF() function:
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.
An example of two dates that'd give a 7-day difference could be:
mysql> select datediff('2011-06-18','2011-06-25');
+-------------------------------------+
| datediff('2011-06-18','2011-06-25') |
+-------------------------------------+
| -7 |
+-------------------------------------+
This means that the first date occured -7 days after the first date; that's 7 days before. If you let the two arguments switch place, the result would be a positive 7.
How about considering using UNIX_TIMESTAMP? It uses the concept of elapsed time.
The "old" way to compare two or more dates is to convert then to an unix timestamp (seconds in float) using strtotime() function. For example:
if((strtotime('2011-05-10') - strtotime('2011-05-01')) > 604800)
{
echo('A week has passed');
}
if((strtotime('2011-06-10') - strtotime('2011-05-01')) > 2629743)
{
echo('A month has passed');
}
Or the "new" way is to use the DateTime class bundled with PHP 5.2 or newer. Have a look at http://php.net/manual/en/book.datetime.php.
And of course date_diff has plenty of examples.
You need to consider what you are exactly looking for.
Are you looking to filter dates older than a week? You can do that comparison on the SQL and you don't burden PHP with date comparisons.
Are you wanting a date difference? Again, I suggest putting it in SQL and just display the result.
This will give you the number of seconds between the two dates:
<?php
$time = '2011-06-20';
$timeDiff = time() - strtotime($time);
echo $timeDiff;
?>
You could divide this value by 86,400 to get the number of days, and so on.