2014-05-06 52 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) –

相關問題