php - 在php中,如何獲得上周的日期範圍?

  显示原文与译文双语对照的内容
143 3

如何在php中獲取上周的日期範圍?

查看我的代碼 bellow:

<?php 
 function get_last_week_dates(){
//how can i get the date range last week?
//ex: today is 2014-2-8
//the week date range of last week should be '2014-1-26 ~ 2014-2-1'
 }
?>
时间:

60 4

你可以使用 strtotime()

$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
echo $start_week.' '.$end_week ;

更新

更改了代碼以處理星期日。如果當天是星期日,那麼- 1周將是上周日,再次獲得上周的星期日。

$previous_week = strtotime("-1 week +1 day");

另外,如果我們需要找到 current weeknext week 日期範圍,我們可以

周-

$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week); 
$end = date("Y-m-d",$end_week); 

下星期英鎊

$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week); 
$end = date("Y-m-d",$end_week); 
原作者:
111 4

簡單使用

date("m/d/Y", strtotime("last week monday"));
date("m/d/Y", strtotime("last week sunday"));

它將給出星期and星期日的日期。

原作者:
59 1

你需要使用 strtotime 函數進行這裡操作

<center>
<?php
 function get_last_week_dates()
 {
//how can i get the date range last week?
//ex: today is 2014-2-8
//the week date range of last week should be '2014-1-26 ~ 2014-2-1'
 $startdate ="last monday";
 if (date('N')!== '1')
 { 
//it's not Monday today
 $startdate. =" last week";
 }
 echo"<br/>";
 $day = strtotime($startdate);
 echo date('r', $day); 
 echo"<br/>";
 $sunday = strtotime('next monday', $day) - 1;
 echo date('r', $sunday);
 }
 get_last_week_dates();
?>
</center>
70 2

為了嘗試解決這個問題的樂趣:

date_default_timezone_set('UTC');
$firstDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-7);
$lastDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-1);
echo("Last week began on:".date("d.m.Y",$firstDayOfLastWeek));
echo("<br>");
echo("Last week ended on:".date("d.m.Y",$lastDayOfLastWeek));
131 4

這應該可以

$startWeek = date('Y-m-d',strtotime("Sunday Last Week"));
$endWeek = date('Y-m-d',strtotime("Sunday This Week"));

如果在星期一運行的話,這將不起作用。it星期日( 前一天) 到下周日。所以使用Abhik的chakraborty方法進行以下操作:

$startTime = strtotime("last sunday midnight",$previous_week);
$endTime = strtotime("next sunday midnight",$startTime);
$startDate = date('Y-m-d 00:00:00',$startTime);
$endDate = date('Y-m-d 23:59:59',$endTime);

這將給出

start = 2014-08-10 00:00:00
endDate = 2014-08-17 23:59:59
...