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;
}
Related
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!
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:
How to get the first day of a given week number in PHP (multi-platform)?
(9 answers)
Closed 9 years ago.
I have a value that is the number for the weekofyear (between 1-52 ). I want to find out the date (yyyy-mm-dd) for the last day of that week.
I would prefer to do it in PHP rathe then MYSQL.
This can be easily solved with DateTime::setISODate() method:
$week = 50;
$dt = new DateTime();
$dt->setISODate($dt->format('o'), $week, 7);
echo $dt->format('Y-m-d');
demo
Or you can just create DateTime object (or unix timestamp with strtotime()) with ISO-8601 format like 2014-W50-7:
$week = 50;
$iso = sprintf("2014-W%02d-7", $week);
$dt = new DateTime($iso);
echo $dt->format('Y-m-d');
echo date('Y-m-d', strtotime($iso)); # or using strtotime()
demo
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
How to get difference between two dates in Year/Month/Week/Day?
i am trying to calculate years and months and days between two given dates in PHP.
i am also using timestamp of those date. is there any way to calculate years and months and
days from difference of those time stamp.
for example first date is 2 Jan, 2008. and second one is 5 July, 2012.
and result is 4 Years 5 monts and 3 days.
i am working on timestamp as date input and want to know that is there any function available which directly calculate above things by two input timestamp
You could use the DateTime object for that (please note the missing "," in the datetime constructor).
$datetime1 = new DateTime('2 Jan 2008');
$datetime2 = new DateTime('5 July 2012');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months and %d days');
You can do this pretty easily with DateTime:
$date1 = new DateTime("2008-01-02");
$date2 = new DateTime("2012-07-05");
$diff = $date1->diff($date2);
echo "difference " . $diff->y . " years, " . $diff->m." months, ".$diff->d." days "
Documentation
You should have a look at Carbon, it's a pretty new PHP 5.3 lib on top of DateTime with a lot of usefull methods.
For Date diff:
<?php
$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2013, 1, 1, 'America/Vancouver');
echo $dtOttawa->diffInHours($dtVancouver);
echo $dtOttawa->diffInDays($dtVancouver);
echo $dtOttawa->diffInMinutes($dtVancouver);
echo $dtOttawa->diffInYears($dtVancouver);
If you want Human readable diff:
$dt = Carbon::createFromDate(2011, 2, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 28 days before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
You can create two DateTime objects (www.php.net/datetime) from the timestamps.
When calling the diff method you get a DateInterval object, which has properties for years and months.
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'))
);