calculate week number from start date - php

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

Related

How to get week number from a date

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;

PHP - Set auction end date to end in a weekday

I'm making an auction house where the auction ends after 1 month of the date it started.
Here's what i got so far:
$added_on = date('Y-m-d H:i:s');
$ends_on = new DateTime('NOW');
$ends_on->add(new DateInterval('P1M')); // 1 month
$ends_on->format('U');
$stringdate = $ends_on->format('Y-m-d H:i:s');
I need the auction to end 1 month after but on a week day, if possible, between 9h-18h. Any idea how to achieve this result?
You need to check the day number of the new $ends_on date i.e. $d = $ends_on->format("w"); If 0 (Sunday) add 1 more day. If 6 (Saturday) add 2 more days. The times will need a bit more as when do you decide the cut off time. If a entry is made after 1800 does it still expire on the same day or the next day?

php get week elapsed

How to get elapsed week with start and end date, and week is started with Monday to Sunday
Example:
start - end = output week
2013-11-20 - 2013-11-24 = 1 week
2013-11-20 - 2013-11-28 = 2 weeks
2013-11-20 - 2013-12-10 = 4 weeks
You can use UNIX like time to easily manipulate things. Assuming $start and $end are in format YYYY-MM-DD
$start = strtotime($start); //To convert it into UNIX time
$end = strtotime($end);
echo "Number of weeks are : ".(($end-$start)/7*24*60*60);
This will print the number of weeks.

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.

Categories