【问题标题】:Get first day of current week - x获取当前周的第一天 - x
【发布时间】:2016-11-29 10:49:18
【问题描述】:

我需要得到一周的第一天(星期一),从今天起 8 周后,其中 8 是一个变量。

在 PHP 中最好的方法是什么?

【问题讨论】:

  • 所以您的意思是说,您需要返回 8 周并从该特定周获得星期一的日期?
  • 是的,正是我的意思。
  • 最好的方法是使用datetime functions或DateTime class
  • @B.1988 跟随链接并理解亚历克斯刚刚提到的东西.. 它肯定会帮助你并让你理解逻辑。一切顺利。

标签: php date datetime strtotime mktime


【解决方案1】:

你可以这样做

echo date("l M-d-Y", strtotime('monday this week'));
echo date("l M-d-Y", strtotime('sunday this week'));
echo date("l M-d-Y", strtotime('monday last week'));
echo date("l M-d-Y", strtotime('sunday last week'));
echo date("l M-d-Y", strtotime('monday next week'));
echo date("l M-d-Y", strtotime('sunday next week'));

您也可以按月搜索

echo date("l M-d-Y", strtotime('first day of this month'));
echo date("l M-d-Y", strtotime('last day of this month'));
echo date("l M-d-Y", strtotime('first day of last month'));
echo date("l M-d-Y", strtotime('last day of last month'));
echo date("l M-d-Y", strtotime('first day of next month'));
echo date("l M-d-Y", strtotime('last day of next month'));

【讨论】:

    【解决方案2】:
    $weeks = 8;
    
    // Timestamp for $weeks weeks ago
    $time = strtotime("$weeks weeks ago");
    
    // Day of the week for $time (1 - Mon, ...)
    $week_day = date('N', $time);
    
    // Number of days from Monday
    $diff = $week_day - 1;
    
    // The date of the Monday $weeks weeks ago
    echo date('j', $time - ($diff * 24 * 3600));
    

    【讨论】:

    • 就我而言,这个答案是最好的。我只将输出日期格式替换为“d/m/Y”。谢谢大家的及时回复!
    【解决方案3】:

    其实并不复杂,你要做的就是玩一下日期时间;

    <?php
    $dt = new Datetime(sprintf('%d weeks ago', 8)); // replace 8 with variable, your value, whatever
    $day = $dt->format('w');
    $dt->modify(sprintf('%d days go', ($day - 1) % 7));
    

    您的$dt 应该具有您所寻求的价值

    【讨论】:

      【解决方案4】:

      您可以在 PHP 中使用日期进行一些数学运算,例如:

      $now = date('F d, Y H:i');
      $newdate = date('F d, Y H:i', strtotime($now.' - 8 weeks'));
      echo $newdate;
      

      在这种情况下,它将输出当前日期减去 8 周。

      还可以使用今天是哪一天来计算:

      $dw = date( "w", strtotime($newdate));
      

      其中$dw 将是 0(星期日)到 6(星期六)更多信息可以找到:PHP: date

      解决方案

      在你的情况下,它看起来如下:

      <?php
      $weeks = 8;
      
      $now = date('F d, Y H:i:s');
      $newdate = date('F d, Y H:i:s', strtotime($now.' - '.$weeks.' weeks'));
      $new_date_day = date( "w", strtotime($newdate));
      $minus = $new_date_day - 1;
      
      if ($minus < 0) { //check if sunday
          $plus = $minus * -1;
          $newdate = date('F d, Y H:i:s', strtotime($newdate.' + '.$plus.' days'));
      } else {    
          $newdate = date('F d, Y H:i:s', strtotime($newdate.' - '.$minus.' days'));
      }
      
      echo $newdate;
      ?>
      

      当然你可以echo 任何你想要的约会风格。前任F d, Y H:i:s 将输出November 28, 2016 06:18:03。

      【讨论】:

        猜你喜欢
        • 2011-02-25
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 2019-04-12
        • 1970-01-01
        • 2019-06-26
        • 1970-01-01
        相关资源
        最近更新 更多