简体   繁体   English

从给定日期获取一周前 PHP

[英]Get a week ago from given date PHP

I have a monthly bill table that stored where customer need to pay the bill in Ymd format, i need to send an email notification a week before this due date.我有一个每月帐单表,其中存储了客户需要以Ymd格式支付帐单的位置,我需要在此截止日期前一周发送电子邮件通知。 for example:例如:

public function cronSendNotif($transaction){
  $dueDate = $transaction->getDueDate(); // 2019-08-03
  $weekAgo = $this->getWeekAgoDate($dueDate); // 2019-07-27

  $this->sendEmailNotification($transaction->getId(),$weekAgo);
}

private function getWeekAgoDate($dueDate){
   // how ??
}

how can i get the week ago date in Ymd format , if i have this given date in Ymd format?如果我有Ymd格式的给定日期,我如何以Ymd格式获取一周前的日期?

Try this:尝试这个:

private function getWeekAgoDate($dueDate){
  $weekAgo = date('Y-m-d', strtotime('-7 days', strtotime($dueDate)));
  return $weekAgo;
}

Using strtotime, you can simply add/subtract days, weeks etc. from a date.使用 strtotime,您可以简单地从日期中添加/减去天、周等。 Example:例子:

function getWeekAgoDate($dueDate){
   return date("Y-m-d", strtotime($dueDate . "-1 week"));
}

You can also make use of the modify function.您还可以使用修改功能。

$weekAgo = getWeekAgoDate($dueDate);
echo $weekAgo->format('Y-m-d H:i:s');

function getWeekAgoDate(DateTime $dueDate){
   return $dueDate->modify("- 7 days");
}

Documentation: php.net/manual/en/datetime.modify.php文档:php.net/manual/en/datetime.modify.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM