如何在php中获取上周的日期范围?
下面是我的代码:
<?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'
}
?>
发布于 2015-03-18 14:53:45
只需使用
date("m/d/Y", strtotime("last week monday"));
date("m/d/Y", strtotime("last week sunday"));
它将给出上周的星期一和星期日的日期。
发布于 2014-02-08 17:05:36
为此,您需要使用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>
发布于 2014-02-08 17:14:15
只是为了解决这个问题的乐趣:
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));
https://stackoverflow.com/questions/21644002
复制相似问题