How to get week number from a date - php

I want to get the week number from a specific date
Like so
if project start date is 01-12-2020,
and today is 14-12-2020
the current week number is 2
thanks

use this First get 1day of a date and get its week no cuz php provide year week no.
now get current date week no and subtract it you will get the current week no:
$month= (date('Y-m-01'));
$yearweek = date('W');
$monthweek = date('W',strtotime($month));
echo $yearweek - $monthweek;

You calculate how many weeks there are between two dates.
To do this, calculate the difference in days and divide that by 7.
The week number (Project week) is then the whole part plus 1.
$start = date_create('01-12-2020');
$curDate = date_create('14-12-2020');
$weekNumber = (int)($start->diff($curDate)->days/7)+1;

You should try with :
$now = Carbon::now();
echo $now->weekOfYear;

Related

How to get the first and last day of the last x (x = 3, 6 or 12) months in php?

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

calculate week number from start date

I have some problem with the week number calculation in PHP/Mysql. here is my need
i have a start date and time ex."2014-09-27 00:00:00" saturday. So this week will be take as
firstweek (ie) 1. If the current date is 2014-10-03 12:16:11 this will be taking as second week or if the current date is 2014-10-08 09:09:12 it will shown as third week. So how can i calculate the week number for the current date from the start date.
2014-09-21 to 2014-09-27 is 1 week
2014-09-28 to 2014-10-04 is 2 week
2014-10-05 to 2014-10-11 is 3 week // It will continue
So please suggest me how can i do that?
You can do this with the DateTime object:
$datetime1 = new DateTime('2014-09-28');
$datetime2 = new DateTime('2014-10-04');
$interval = $datetime1->diff($datetime2);
// Output the difference in days, and convert to int
$days = (int) $interval->format('%d');
// Get number of full weeks by dividing days by seven,
// rounding it up, and adding one since you wanted start
// day to be week one.
$weeks = ceil($days / 7) + 1;
Here's a phiddle to demonstrate: http://phiddle.net/4

PHP date interval - wrong month difference

Have a look at this code:
$first = DateTime::createFromFormat('Y-m', '2001-07');
$last = DateTime::createFromFormat('Y-m', '1998-06');
$interval = $first->diff($last);
echo "m diff: ".$interval->m." y diff: ".$interval->y."\n";
The output is m diff: 0 y diff: 3
Why does it return a wrong month difference?
Interesting that if I change dates as '2001-08' and '1998-07', it returns a correct month interval ==1.
Thanks!
PHP DateTime doesn't handle incomplete datetimes.
DateTime::createFromFormat('Y-m', '2011-07') gives a DateTime that has a year of 2011, a month of 7, and a day, hour, minute and second taken from the current time (at the moment I write this, 2011-07-31 18:05:47.
Likewise, DateTime::createFromFormat('Y-m', '1998-06') gives a DateTime that has a year of 1998, a month of 6, and a day, hour, minute, and second taken from the current time. Since June 31st is a nonexistent date, the result is 1998-07-01 18:05:47 (31 days after the day before June 1st).
The difference between those two dates is then 3 years, 0 months, and 30 days.
In your example of 2001-08 and 1998-07, both months happen to have a 31st day, so the math comes out correctly. This bug is a difficult one to pin down, because it depends on the date that the code is run even though it doesn't obviously appear to.
You could probably fix your code by using a format of "Y-m-d H:i:s" and appending "-01 00:00:00" to each date you pass to createFromFormat, which will anchor the DateTime you get back to the beginning of the month.
I know this is old, maybe this may help someone out there:
$first = DateTime::createFromFormat('Y-m', '2001-07');
$last = DateTime::createFromFormat('Y-m', '1998-06');
$interval = $first->diff($last);
$num_months = (($interval->y) * 12) + ($interval->m);
Explanation : convert $interval->y which is the year to months by multiplying it by 12 and add the succeeding months which is $interval->m

php week interval from date ("W")

Hy, I have in database number of the week date ("W") and I want to display week interval like 28 Jan -> 3 Feb in this format, and I don't know if it's possible. Can you help?
Thanks!
Try this
$year = 2013;
$week_no = 6;
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
$week_end = clone $week_start;
$week_end = $week_end->add(new DateInterval("P1W"));
echo $week_start->format('d-M-Y') . " - ".$week_end->format('d-M-Y');
Transform your intervals into timestamps.
If its not the first day of the week get the first day of that week with strtotime "last sunday" (or monday) for the first date.
Do the same for the second date this time geting the last day of the week with "next saturday" (or sunday);
Get both dates W and make a mysql comparison between the weeks.

[mysql&php], what is the first day of a week, Monday or Sunday?

What is the first day of the week in mysql, Monday or Sunday??
I want to do this: (i am using php btw)
Get today date (no problem)
Display information of this week (stuck here)
Previous and next week button, to display previous and next week data (cant do anything here)
I am kinda of stuck while playing with the "date" thing. Can anyone help?
Monday.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_weekday
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
To get details about previous or next weeks you can use
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
eg SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 WEEK);
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
To get the weekday in PHP you can use the following:
<?php
$datetime = new DateTime('2010-12-31 23:59:59');
$today = $datetime->format('l');
echo $today;
?>
http://www.php.net/manual/en/function.date.php

Categories