0

我正在使用时间戳对数据库进行查询。现在我正在使用硬编码日期进行查询,

$dd1 = 20;//date("d",$ddb1);
    $mm1 = date("m");
    $yyyy1 = date("Y");
    $dd2 = 26;//date("d",$ddb2);
    $mm2 = date("m");
    $yyyy2 = date("Y");

但是每当我运行脚本时,我都需要代码来计算上周的数据(周一到周日)。例如,我今天(Wensday)运行脚本,所以计算的时间应该是上周从(周一到周日)。在本周的任何其他日子里,直到周日晚上 12:00:01 之前,都应该有相同的结果。

试图做类似的事情:

    //$ddb1 = time() - ($argv[1] * 24 * 60 * 60);
    //$ddb2 = time() - ($argv[2] * 24 * 60 * 60); 

从当前日期减去天数,但必须有一种自动方法来执行此操作。

4

4 回答 4

3
$first_day = strtotime('Last Week');
$last_day = strtotime('Last Sunday');

echo "Last Monday was ".date('m-d-Y', $first_day);
echo "Last Sunday was ".date('m-d-Y', $last_day);

#Last Monday was 08-27-2012
#Last Sunday was 09-02-2012
于 2012-09-05T11:58:52.910 回答
2

为什么不直接使用strtotime

$lastsunday=strtotime("last Sunday");
$lastmonday=strtotime("last Sunday")-(7*86400);

编辑:

<?php
    date_default_timezone_set('Australia/NSW');
    $lastSunday=strtotime("last Sunday");
    $lastMonday=strtotime("last Sunday")-(6*86400);

    echo "Last Sunday was ".date('d-m-Y', $lastSunday)."<br>";
    echo "Last Monday was ".date('d-m-Y', $lastMonday);

?>  

输出:

Last Sunday was 02-09-2012
Last Monday was 27-08-2012 
于 2012-09-05T11:59:17.727 回答
1

获取最后一个星期日:

$last_sunday = strtotime('Last Sunday');

然后减去 6 天得到最后一个星期一:

$last_monday = date($last_sunday-86400*6)
于 2012-09-05T12:02:34.480 回答
0

试试这个:-

/**
* int nth_day_of_month(int $nbr, str $day, int $mon, int $year)
*   $nbr = nth weekday to find
*   $day = full name of weekday, e.g. "Saturday"
*   $mon = month 1 - 12
*   $year = year 1970, 2007, etc.
* returns UNIX time
*/

function nth_day_of_month($nbr, $day, $mon, $year)
{
   $date = mktime(0, 0, 0, $mon, 0, $year);
   if($date == 0)
   {
      user_error(__FUNCTION__."(): Invalid month or year", E_USER_WARNING);
      return(FALSE);
   }
   $day = ucfirst(strtolower($day));
   if(!in_array($day, array('Sunday', 'Monday', 'Tuesday', 'Wednesday',
         'Thursday', 'Friday', 'Saturday')))
   {
      user_error(__FUNCTION__."(): Invalid day", E_USER_WARNING);
      return(FALSE);
   }
   for($week = 1; $week <= $nbr; $week++)
   {
      $date = strtotime("next $day", $date);
   }
   return($date);
} 


function getWeekNoByDay($year = 2007,$month = 5,$day = 1) {
    return ceil(($day + date("w",mktime(0,0,0,$month,1,$year)))/7);
}
于 2012-09-05T12:15:58.867 回答