如何在php中获得上周的日期范围? [英] how can I get last week' date range in php?

查看:113
本文介绍了如何在php中获得上周的日期范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

看到我的代码如下:

 <?php 
函数get_last_week_dates(){
//如何获取上周的日期范围?
// ex:today is 2014-2-8
//上周的周日期范围应为'2014-1-26〜2014-2-1'
}
?>


解决方案

您可以使用 strtotime()

  $ previous_week = strtotime( -  1周+1天); 

$ 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周+1天); 




此外,如果我们需要找到本周下周日期
范围我们可以作为


本周 -

  $ d = strtotime(today ); 
$ start_week = strtotime(最后星期日午夜,$ d);
$ end_week = strtotime(next saturday,$ d);
$ start = date(Y-m-d,$ start_week);
$ end = date(Y-m-d,$ end_week);

下周 -

  $ d = strtotime(+ 1周-1天); 
$ start_week = strtotime(最后星期日午夜,$ d);
$ end_week = strtotime(next saturday,$ d);
$ start = date(Y-m-d,$ start_week);
$ end = date(Y-m-d,$ end_week);


how can I get last week' date range in php ?

see my codes 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'
    }
?>

解决方案

You can use 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 ;

UPDATE

Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.

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

In addition if we need to find the current week and next week date range we can do as

Current 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);  

Next 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); 

这篇关于如何在php中获得上周的日期范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆