14

Possible Duplicate:
how to find first day of the next month and remain days to this date with php

I have this to get 1st day of current month

$_SERVER['dataInicio'] = date('Y') . "-" . date('m') . "-" . "1";

I need something similar to get 1st day of NEXT month

Community
  • 1
  • 1
CMartins
  • 3,247
  • 5
  • 38
  • 53

3 Answers3

37
$date = new DateTime('now');

$date->modify('first day of next month');
echo $date->format('D') . '\n';

$date->modify('first day of this month');
echo $date->format('D') . '\n';
Pere
  • 1,647
  • 3
  • 27
  • 52
rscata
  • 491
  • 5
  • 5
7

This seems like a duplicate or similar post to this for the next month

but for the next month something like this if you want to use strtotime

date("m/l/Y", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));

this will return

12/Saturday/2012 if you want just the days name just set the date format to l (lower case L)

date("l", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));

How to find first day of the next month and remaining days till this date with PHP

or this

In PHP, is there an easy way to get the first and last date of a month?

Community
  • 1
  • 1
Clark T.
  • 1,470
  • 2
  • 11
  • 25
  • 2
    I would do it like this instead, it won't fail in December. strtotime(date('m', strtotime('+1 month')) . '/01/' . date('Y', strtotime('+1 month'))); – Halfstop Dec 06 '16 at 16:51
  • @Halfstop great point i'll update my answer – Clark T. Dec 06 '16 at 17:49
  • I run this code on 2017-DEC-15 and its return 01/Sunday/2017 so it came me to 12 month back it not move me to 2018. so this technique not gives the correct output. i found a solution for it: $date_obj=date_create("first day of next month"); echo $date = date_format($date_obj,"Y-m-d H:i:s"); – Muhammad Nadeem Dec 15 '17 at 13:17
  • I use a variation of this: date('Y-m-01', strtotime(date('Y-m-d') . ' + 1 month')) – darktideac Nov 27 '18 at 20:38
  • strtotime('first day of next month'); Wouldn't this be the easiest thing to use? – danmentzer Mar 25 '19 at 20:41
4
$_SERVER['dataInicio'] = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));

Should do what you're wanting.

Benny Hill
  • 6,191
  • 4
  • 39
  • 59