在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