Skip to content

Instantly share code, notes, and snippets.

@marcusedu
Last active July 23, 2018 20:26
Show Gist options
  • Save marcusedu/2fb11287dd8021ad0cdeac30497784b4 to your computer and use it in GitHub Desktop.
Save marcusedu/2fb11287dd8021ad0cdeac30497784b4 to your computer and use it in GitHub Desktop.
<?php
/**
* Description of MyDateAnalytics
* Esta classe é uma ajuda ao comparar se uma data,
* está nesse semana, semana passada, neste mês ou
* no mês passado
* @author marcus eduardo marcusedu7@gmail.com
*/
class MyDateAnalytics {
private $now;
private $startWeek;
private $lastWeek;
private $startLastWeek;
private $startMonth;
private $lastMonth;
private $startLastMonth;
function __construct() {
$this->now = new \DateTime();
$this->startWeek = new \DateTime("sunday last week");
$this->lastWeek = new \DateTime("-7 day");
$this->startLastWeek = new \DateTime("sunday last week");
$this->startLastWeek->modify("-7 day");
$this->startMonth = new \DateTime("first day of this month");
$this->lastMonth = new \DateTime("-1 month");
$this->startLastMonth = new \DateTime("first day of last month");
}
public function isThisWeek($date) {
$dateAnal = new \DateTime($date);
return ($dateAnal > $this->startWeek && $dateAnal < $this->now);
}
public function isLastWeek($date) {
$dateAnal = new \DateTime($date);
return ($dateAnal > $this->startLastWeek && $dateAnal < $this->lastWeek);
}
public function isThisMonth($date) {
$dateAnal = new \DateTime($date);
return ($dateAnal > $this->startMonth && $dateAnal < $this->now);
}
public function isLastMonth($date) {
$dateAnal = new \DateTime($date);
return ($dateAnal > $this->startLastMonth && $dateAnal < $this->lastMonth);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment