php - How do I get next month date from today's date and insert it in my database? -
i have 2 columns in db: start_date
, end_date
, both date
types. code updating dates 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."' `users`.`id` ='".$id."' limit 1 ;";
what best way make $end_date
equal $start_date
+ 1 month? example, 2000-10-01 become 2000-11-01.
you can use php's strtotime() function:
// 1 month today $date = date('y-m-d', strtotime('+1 month')); // 1 month specific date $date = date('y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
just note +1 month
not calculated intuitively. appears add number of days exist in 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 can use:
$date = date('y-m-d'); // initial date string 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)));
Comments
Post a Comment