Get 1st day of next month [duplicate] - php

This question already has answers here:
Closed 10 years ago.
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

$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';

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?

$_SERVER['dataInicio'] = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));
Should do what you're wanting.

Related

Getting first day of the month, is outputting the wrong first day of the month

I want to display the first day of the month in 'day of the week' format.
For example, The below code should select the month of August and say the day of the week 01 falls on which is Thursday, but for some reason, the below code outputs Friday which is wrong, on some months such as May it correctly says Wednesday.
$monthNumber = 3;
$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
$dateTest = date("Ym" . 1, strtotime($monthNumber . " month", $base));
$unixTimestamp = strtotime($dateTest, $base);
echo date("l", $unixTimestamp);
Does anyone have any ideas to make it show the correct day?
$base fixes a bug with it not showing the correct month.
And why won't you use the dateTime?
echo (new \DateTime('first day of august 2019'))->format('l');
Read more about Relative formats.
You have to use date() at first:
$base = date('Y-m',time()) . '-01 00:00:00';
And dont have to set the first second :)
To get the day of the week falling on the first day of any month you can use the prefix first day of e.g.
echo (new \DateTime('first day of August'))->format('l');
This will show correctly Thursday for August 2019.
If you want to do it dynamically using numbers for months, you can create a dummy date like you were trying to do.
$timeString = sprintf('01.%02d.%04d', 8, 2019); // replace the numbers with your variables
echo (new \DateTime($timeString))->format('l');

Start Day and End Day Of Current Month & Start Day & End Day of Current Year [duplicate]

This question already has answers here:
The first day of the current month in php using date_modify as DateTime object
(13 answers)
Closed 9 years ago.
I'm trying to get the current start date of the month and current end date of the month.
It needs to change for each month, so for example for this month I need:
$startMonth = 2014-02-01
$endMonth = 2014-02-28
I'm aware of the mktime PHP page but I can't get my head around it: http://uk3.php.net/mktime
I also need the start day and end day of the current year:
$startYear = 2014-01-01
$endYear = 2014-12-31
I need it in the above format, I can get the previous months using:
$previousmonthStart = date("Y-m-d", mktime(0, 0, 0, date("m"), 0, date("Y")));
$previousmonthEnd = date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1, date("Y")));
Just need to know what changes what.
Since you want to use mktime()...
I think the first day of the year and the last day of the current year are always (no mktime() needed):
date('Y') . '01-01';
date('Y') . '12-31';
For the first and last of the current month with mktime() try this:
$m = (integer) date('n');
$start = date('Y-m-d',mktime(1,1,1,$m,1,date('Y')));
$end = date('Y-m-d',mktime(1,1,1,++$m,0,date('Y')));
//Considering today is 2014-02-05
echo $start; //2014-02-01
echo $end; //2014-02-28
mktime wants hour, min, sec, month, day ,year
$m tells mktime to use the current month for the month value and the next 1 tells it to use the first day of that month.
++$m tells it to use the next month and the 0 gets you the day before the first day of the next month which is the last day of the current month.
Example: http://codepad.org/1G7UJNni

Get the last day of the month? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP last day of the month
Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?
$start = new DateTime('2012-02-01');
$end = clone $start;
// Interval = last day of the month minus current day in $start
$interval = $start->getLastDayOfMonth() - intval($start->format('j'));
$end->add(new DateInterval('P' . $interval . 'D'));
EDIT: thanks, voted to close, it's a duplicate, sorry for asking...
The php date function gives you the number of days in the month with 't'
date("t");
See: http://php.net/manual/en/function.date.php
It's simple to get last month date
echo date("Y-m-t", strtotime("-1 month") ) ;
echo date("Y-m-1", strtotime("-1 month") ) ;
at March 3 returns
2011-02-28
2011-02-1
t gives you the total number of days in the current month. j gives you the current day of the month.
Using modify and some subtraction from format-ing the datetime, you can get to the end of the month.
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);

Finding First day of week via php [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Get first day of week in PHP?
Hi,
I want to find first and last date of current week and last week.
Similarly I want to find first and last date of current month and last month.
This has to be done in PHP. Please help.
strtotime is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Here's a function for the first and last day of the week:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
It can probably be adapted to do month as well, but I wanted to get my answer in! :)

How can I create a function that can figure out the previous month's last day dates?

I have a PHP calendar that lists all of the days of the month in a table. Before the first day of the month I have numbers from the prior month and after the last day of the month are the numbers of the days for the upcoming month.
Here's a photo of the Calendar as it currently looks. As you can see the bottom gray numbers are working fine, but the numbers preceding the first day of the month are negative numbers and should instead appear as '29,30'
The numbers after the last day of the month were simply '32,33,34' for example, so I just created an if statement that checks if the number is greater than the total numbers of days in the current month and if so, then subtract the total numbers of days in the month from '32' for example, which would then make it appear as '1,2,3'.
if ($day > $total_days_of_current_month) {
echo '<td>' . ($day - $total_days_of_current_month) . ' </td>'; // for example,33-31=2
}
My problem is creating an if statement that somehow knows what the last days of the prior month was. The problem is that some months have 30 days and some have 31 days. Also, the month of February and leap years are a problem. Does anyone know an if statement so i can make it appear as '28,29,30' from the previous month?
Perhaps this can help?
cal_days_in_month()
(Edited to include inshalla's feedback)
Assuming $day is a UNIX timestamp:
// gets you the first of the month
$date = getdate(mktime(0, 0, 0, date('n', $day), 1, date('Y', $day)));
From here, do one of these:
$lastDayOfPrevMonth = strtotime('-1 day', $date[0]);
// or, better, get the first day "in the grid" (assuming week starts on Sunday)
$date = strtotime("-$date[wday] days", $date[0]);
I don't want to harp on it, but have you looked at my previous answer? That's a more robust way to build a calendar instead of all this special case checking.
Why not just take the timestamp of the first day of the month, and subtract 24 hours from it and then use date('d', $timestamp); ? Also, if you have a timestamp in that month, date('t', $timestamp); will get the number of days in the month for you.
A quick example:
// sets to 3am, first day of month, 3am helps us avoid DST issues with subtracting hours
$monthStart = mktime(3,0,0,date('n'),1,date('Y'));
$thisMonth = date('n', $monthStart);
// rewind to the sunday before- date('w') is the "weekday" 0 based
$date = $monthStart - (date('w',$monthStart) * 60 * 60 * 24);
while ($date<$monthStart || date('n', $date) == $thisMonth)
{
echo "<tr>";
for ($x=0; $x<7; $x++) {
echo "<td";
if (date('n', $date) != $thisMonth) echo " class='notMonth'";
echo ">";
echo date("j", $date);
echo "</td>";
$date += 60*60*24;
}
echo "</tr>\n";
}
Take a look at the source of PHP-Calender-Class Project:
http://code.google.com/p/php-calendar-class/
class source: http://code.google.com/p/php-calendar-class/source/browse/trunk/calendar.class.php
It may give you some insight on the problem.
Using cal_days_in_month(), How about adding the following code:
if ($day < 1) {
echo '<td>' . ($day + cal_days_in_month(
CAL_GREGORIAN,date_format("n",
date_add(date_create(),"P-1M")))) . ' </td>'; // for example,-1 + 30 = 29th
}
EDITED: Added the date_format function
Wait a minute... your month name at the top is wrong! It says "August 2009", but the month has 30 days and the weekdays are right for September 2009!
You need to repair the current month first, before worrying about other months.

Categories