2014-05-06 106 views
0

在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' 
+0

让我们看看你已经尝试什么 –

+0

什么是根据您一周的第一天? –

+0

http://php.net/manual/en/function.date.php - 你是否试图自己解决这个问题? – ChaseC

回答

1

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 

Demo

0
$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

+0

如果是星期一或星期二,“下个星期一”会不会给你错误的一天? [演示](http://codepad.viper-7.com/8WtKVM) –

相关问题