Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get next month date from today's date and insert it in my database?

Tags:

date

php

mysql

I have two columns in my db: start_date and end_date, which are both DATE types. My code is updating the dates as follows:

$today_date = date("Y-m-d"); $end_date = date("Y-m-d"); // date +1 month ??  $sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."'  WHERE `users`.`id` ='".$id."' LIMIT 1 ;"; 

What is the best way to make $end_date equal to $start_date + one month? For example, 2000-10-01 would become 2000-11-01.

like image 348
Saleh Avatar asked Nov 30 '10 21:11

Saleh


People also ask

How to get next month from date in PHP?

date('Y-m-t', strtotime('+1 month'));

How do you get the month from a timestamp field?

Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column. (In our example, we use the start_date column of date data type).

How can I get tomorrow date in PHP?

$newDate = date('Y-m-d', strtotime('tomorrow')); echo $newDate; ?>


2 Answers

You can use PHP's strtotime() function:

// One month from today $date = date('Y-m-d', strtotime('+1 month'));  // One month from a specific date $date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01'))); 

Just note that +1 month is not always calculated intuitively. It appears to always add the number of days that exist in the current month.

Current Date  | +1 month ----------------------------------------------------- 2015-01-01    | 2015-02-01   (+31 days) 2015-01-15    | 2015-02-15   (+31 days) 2015-01-30    | 2015-03-02   (+31 days, skips Feb) 2015-01-31    | 2015-03-03   (+31 days, skips Feb) 2015-02-15    | 2015-03-15   (+28 days) 2015-03-31    | 2015-05-01   (+31 days, skips April) 2015-12-31    | 2016-01-31   (+31 days) 

Some other date/time intervals that you can use:

$date = date('Y-m-d'); // Initial date string to use in calculation  $date = date('Y-m-d', strtotime('+1 day', strtotime($date))); $date = date('Y-m-d', strtotime('+1 week', strtotime($date))); $date = date('Y-m-d', strtotime('+2 week', strtotime($date))); $date = date('Y-m-d', strtotime('+1 month', strtotime($date))); $date = date('Y-m-d', strtotime('+30 days', strtotime($date))); 
like image 94
Gazler Avatar answered Oct 10 '22 04:10

Gazler


The accepted answer works only if you want exactly 31 days later. That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.

If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.

$date = date("Y-m-01"); $newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ; 

This way, you will be able to get the month and year of the next month without having a month skipped.

like image 41
Patrick Desjardins Avatar answered Oct 10 '22 05:10

Patrick Desjardins