How to get month number by week number and year [duplicate] - php

This question already has answers here:
Month by week of the year?
(4 answers)
Closed 6 years ago.
I want to find the month number by given year and given week number,
example- if my year: 2017 and week number is: 25 what will be the month.

Try this:
$date = new DateTime( "01.01.2017" ); // set datetime to 1, January 2017
$date->modify( "+21 weeks" ); // add 21 weeks
echo $date->format( "m" ); // echo month

Related

PHP | Take day of a date, compare it to the current month and transform it into the previous month [duplicate]

This question already has answers here:
How can i parse a date to same day of previous month?
(2 answers)
Closed 2 years ago.
If I have a date that equals: 2020-04-21
and I want to take the day of this date: 21
take the current month: December
calculate it as if it were December then: 2020-12-21
and transform it into the previous month, keeping day and year, then: 2020-11-21
how can I do?
<?
$time=strtotime('2020-04-21'); // get unixtime of custom date
$day=date('d', $time); // get day of custom date
$year=date('Y', $time); // get year of custom date
$month=date('m'); // get current month
$time_first=mktime(0, 0, 0, $month, $day, $year); // get unixtime for first
$time_second=strtotime('-1 month', $time_first); // get unixtime previous month
$date_first=date('Y-m-d', $time_first); // string format of first date
$date_second=date('Y-m-d', $time_second); // string format of second date
var_dump($date_first);
var_dump($date_second);
?>

Missing months with less than 31 days (strtotime) [duplicate]

This question already has answers here:
Months with less than 31 days omitted from list
(3 answers)
Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31
(5 answers)
Month duplication occurs when DateTime called on end of a 31 day month
(1 answer)
Closed 5 years ago.
I want to output the last 13 months, beginning with last month.
my code:
for($i=1;$i<=(13);$i++)
{
echo date("m",strtotime("-".$i." month"));
}
It worksfine, but today (31.) it looks like this
07
07
05
05
03
03
01
12
12
10
10
08
07
I am missing the months, that do not have 31 days. How can I fix this?
Works with a DateTime object! ;-)
$date = new DateTime('2017-08-31');
for ($i=1; $i<=(13); $i++) {
$tmpDate = clone $date;
$tmpDate->modify('last day of -' . $i . ' month');
echo $tmpDate->format('Y-m-d') . "\n";
}
The trick is to copy the variable so that you can reduce the months times var $i. Using "last day of -x month" always picks the last day of that month.
Reference: https://secure.php.net/manual/en/class.datetime.php

GET ISO WEEK NO from month and week no [duplicate]

This question already has answers here:
Calculating days of week given a week number
(14 answers)
Closed 6 years ago.
I am getting month and its weekno like 1,2,3,4,5 . I want ISO week from it.
Eg. I have April as month and week no 4. I want weekno 16 as output.
Thanks in advance.
You can instantiate a new DateTime using the month, increment by the number of weeks, and use the W date format.
<?php
$month = 'April';
$week = 4;
$date = new DateTime("$month 01");
$date->modify("+$week weeks");
echo $date->format('W');
As far as I can tell, the 4th week of April would be ISO week 17, not 16 though.
date("W"); Will return the week number.
http://php.net/manual/en/function.date.php
Use mktime to set a specific date / time and you'll be sorted!

How To Add +6 Months in current date [duplicate]

This question already has answers here:
Add six months in php
(5 answers)
Closed 6 years ago.
i want to add 6 months in current date. i have used the following date format.
$current_time = date('Y-m-d');
i have proceed with this method shown below but an error is occured.
'A non well formed numeric value encountered'.
and print this value
[expiring_plan_date] => 1970-01-02
$data['PlanPayment']['expiring_plan_date'] = date
(
"Y-m-d",
strtotime("+6 month", $current_time)
);
please help..
Remove comma from strtotime
$data['PlanPayment']['expiring_plan_date'] = date("Y-m-d", strtotime("+6 month $current_time));

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

Categories