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!
Related
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);
?>
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
How can I get the first and last day of the last x (I'll replace x by 3, 6 and 12) months ? I know that for the last month will be : date("Y-n-j", strtotime("first day of previous month"));
You can use the DateTime object:
$myDate = new \DateTime("last day of next month");
$format = $myDate->format("Y-n-d");
It returns: 2015-11-30
You can find Relative Formats here: http://php.net/manual/en/datetime.formats.relative.php
Hope it will help you
You already know that a month starts on the 1st. The t specifier to date gives you the number of days in a given month. You can combine the two of those for a given month to determine the first and last day. From there, you would just need to add/subtract 3, 6, and 12 months but that exercise is up to you.
<?php
$first_of_this_month = date("Y-m-01");
$last_of_this_month = date("Y-m-t")
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'))
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get Start and End Days for a Given Week in PHP
Example i use function date('W') will get week of the year.
Suppose get week number 37. Can I get what days in this week Example 2011-09-12 to 2011-09-18
Thank you
You can use the DateTime and DatePeriod classes to work with dates and periods of time.
// ISO week to get the days of
$week = '2011W37';
// Date period for the days in the above week
$period = new DatePeriod(new DateTime($week), new DateInterval('P1D'), 6);
foreach ($period as $day) {
echo $day->format('Y-m-d') . PHP_EOL;
}