在PHP中执行此操作最简单的方法是什么?在PHP中查找当前周和前一周的日期范围
我需要在SQL查询中使用变量。
例如,今天是2015年5月6日。
$current_monday = '05-MAY-2014'
$current_friday = '09-MAY-2014'
$last_monday = '28-APR-2014'
$last_friday = '02-MAY-2014'
在PHP中执行此操作最简单的方法是什么?在PHP中查找当前周和前一周的日期范围
我需要在SQL查询中使用变量。
例如,今天是2015年5月6日。
$current_monday = '05-MAY-2014'
$current_friday = '09-MAY-2014'
$last_monday = '28-APR-2014'
$last_friday = '02-MAY-2014'
DateTime()
DateTime()
接受相对格式,这使得这很容易做到:
$current_monday = (new DateTime('Monday this week'))->format('d-M-Y');
$current_friday = (new DateTime('Friday this week'))->format('d-M-Y');
$last_monday = (new DateTime('Monday last week'))->format('d-M-Y');
$last_friday = (new DateTime('Friday last week'))->format('d-M-Y');
05-May-2014
09-May-2014
28-Apr-2014
02-May-2014
$nextMonday = new DateTime('next monday');
$nextFriday = new DateTime('next friday');
$thisMonday = new DateTime('this monday');
$thisFriday = new DateTime('this friday');
$lastMonday = new DateTime('last monday');
$lastFriday = new DateTime('last friday');
我们在做这个mysql的使用,只需添加->format('Ymd')
...如。 $thisMonday->format('Ymd');
这将返回日期的20140506
的格式,它是友好的日期时间列在MySQL
如果是星期一或星期二,“下个星期一”会不会给你错误的一天? [演示](http://codepad.viper-7.com/8WtKVM) –
不知道你的问题是什么, 但也许你可以试试:
$time = time(); //today
$tmr = $time+(24*60*60);
echo date('Y-m-d', $tmr);
也看到的strtotime()http://php.net/manual/en/function.strtotime.php
strtotime("+1 week");
让我们看看你已经尝试什么 –
什么是根据您一周的第一天? –
http://php.net/manual/en/function.date.php - 你是否试图自己解决这个问题? – ChaseC