我在PHP / Mysql中的周数计算有问题。这是我的需要
我有一个开始日期和时间ex。“2014-09-27 00:00:00”周六。所以本周将采取 第一周(即)1。如果当前日期是2014-10-03 12:16:11,这将是第二周或如果当前日期是2014-10-08 09:09:12它将显示为第三周。那么如何计算从开始日期开始的当前日期的周数。
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
所以请建议我怎么做?
答案 0 :(得分:1)
您可以使用DateTime对象执行此操作:
$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;
这是一个证明:http://phiddle.net/4
的小提琴