Getting first day of the month -1 month [duplicate] - php

This question already has answers here:
how to get the first and last days of a given month
(10 answers)
Closed 9 years ago.
How do I get the first day of the month in php? And the first day of one month early.
So as example:
$startdate = 01-05-2014
$enddate = 01-06-2014
The enddate has to be the first day of the current month so if it is 26-01-2014 the enddate is 01-01-2014
and the startdate is then 01-12-13

As soon as you have the dates as a Timestamp, you can use this to get the first day of the month:
date("Y-m-1", $timestamp) ;
You can get the last day of a month, when you use:
date("t", $timestamp) ; // would give you 31 in January
In a complete example for your problem:
$timestamp = strtotime("26-01-2014") ;
$startdate = date("Y-m-1", strtotime("-1 month", $timestamp)) ; // to subtract one month -> use strtotime
$enddate = date("Y-m-1", $timestamp) ;
See also in the documentation of PHP for strtotime and date.

Related

Current month Minus 1 month [duplicate]

This question already has answers here:
PHP subtract 1 month from date formatted with date ('m-Y')
(11 answers)
Closed 5 years ago.
How do i properly minus 1 month for the current month ?
$current_month1 = date('m');
$current_month = $current_month1-1;
echo $current_month;
//current ouput
6
//desired output
06
check the following:
$now = new \DateTime("now");
$past = $now->modify("-1 month");
DateTime::modify docs
Also you can do it using DateInterval, the docs has example.
You can use date in combination with strtotime for this.
echo date('m', strtotime('last month')); // 06
The m operator in date will get you:
echo date('m', strtotime('now - 1 month'));
Gives 06.

Calculate maximum week number in PHP [duplicate]

This question already has answers here:
PHP - get last week number in year
(7 answers)
Closed 7 years ago.
I am searching for a way to calculate the maximum week number for a given year in PHP. Since the last week number of a year is defined by whichever week has the first Thursday in the new year, I am not really sure how to do it.
date('W', strtotime( $year.'-12-31 23:59:59'));
could also already return 1, if the 31st of December is a Monday, Tuesday or Wednesday. So I was thinking, maybe do it like this?
date('N', strtotime( $year.'-12-31 23:59:59')) <= 3 ? 52 : 53;
i.e., checking if the last day of the year is a Monday, Tuesday or Wednesday and if so, it's 52 week year, otherwise a 53 week year. Not sure if that's the correct way.
To get the ISO week number (1-53), use :-
idate('W', $timestamp)
or use this :-
$date1 = "2015-12-12";
$date = new DateTime($date1);
$week = $date->format("W");
echo "total week: $week";
or try this :-
date("W", strtotime('2015-12-12'))

how to format an integer into the name of a weekday in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is there a way to use DateTime::CreateFromFormat to convert a 0-6 integer into the name of a day of the week?
I am creating a calendar for a script and would like to let users customize the way the days of the week display (Mon vs Monday) so using an array of the week day names is not desirable.
How can I use CreateFromFormat to do this?
Step 1:
Get timestamp of first day of week +X (our integer) days.
// assuming monday = 0, sunday = 6
$day = 2; // our integer (wednesday)
// get timestamp of most recent monday +X days
$ts = (date('N') == 1) ? strtotime("today +$day days") : strtotime("last monday +$day days");
Step 2:
Print the short & long names of the weekday.
echo date('l', $ts); // Wednesday
echo date('D', $ts); // Wed
Hope I understand your question.
$day = 0;
$date = DateTime::createFromFormat('j-M-Y', $day . '-Feb-2010');
echo $date->format('l');
would output :
Sunday

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

Categories