Reputation: 725
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?
Upvotes: 1
Views: 286
Reputation: 19212
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
Upvotes: 1