Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > dk.edb.database > #1387

Re: PHP/MySQL problem med timestamp

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups dk.edb.database
Subject Re: PHP/MySQL problem med timestamp
Date 2018-01-16 19:56 -0500
Organization Aioe.org NNTP Server
Message-ID <p3m70b$nti$1@gioia.aioe.org> (permalink)
References <5a5a0fad$0$726$14726298@news.sunsite.dk> <p3dnfb$lcl$1@gioia.aioe.org> <5a5cd9a1$0$735$14726298@news.sunsite.dk> <p3im97$147v$1@gioia.aioe.org> <5a5e6e4c$0$736$14726298@news.sunsite.dk>

Show all headers | View raw


On 1/16/2018 4:27 PM, Dennis Munding wrote:
> Arne Vajhøj wrote:
>> Nu er jeg heller ikke PHP guru. Onde tunger vil påstå at jeg
>> programmerer Java i PHP.
>>
>> :-)
>>
>> Jeg kunne godt indsætte nogle kommentarer i kodeb, hvis det
>> vil hælpe dig til at forstå den.
> 
> 
> Det vil jeg da ikke takke nej til. :-)

<?php
/**
  * Class AnniversaryPolicyItem hold information about a single 
anniversary item:
  *    - textual description
  *    - interval that triggers anniversary
  */
class AnniversaryPolicyItem {
     public $descrip;
     public $interval;
     /**
      * Constrcut instance.
      * @param string $descrip textual description
      * @param DateInterval $interval interval that triggers anniversary
      */
     public function __construct($descrip, $interval) {
         $this->descrip = $descrip;
         $this->interval = $interval;
     }
}

/**
  * Class AnniversaryPolicy holds multiple instances of 
AnniversaryPolicyItem
  * and can test if a given start date has any anniversaries.
  * @author arne
  *
  */
class AnniversaryPolicy {
     private $before;
     private $after;
     private $format;
     private $annlist;
     /**
      * Construct instance.
      * @param integer $daysbefore number of days before actual 
anniversary to start triggering
      * @param integer $daysafter number of days after actual 
anniversary to start triggering
      * @param string $format dormat to use to displayt date
      */
     public function __construct($daysbefore, $daysafter, $format = 
'd/m/Y') {
         $this->before = new DateInterval('P' . $daysbefore . 'D');
         $this->after = new DateInterval('P' . $daysafter . 'D');
         $this->format = $format;
         $this->annlist = array();
     }
     /**
      * Add interval that triggers anniversary.
      * @param string $descrip textual description
      * @param DateInterval $interval interval that triggers anniversary
      */
     public function add($descrip, $interval) {
         $this->annlist[] = new AnniversaryPolicyItem($descrip, new 
DateInterval($interval));
     }
     /**
      * Get any anniversary for the specified start date
      * @param integer $ts start date as Unix timestamp (seconds since 
1/1/1970)
      * @return string|NULL anniversary information if any, null of no 
anniversary
      */
     public function getAnniversary($ts) {
         // construct range $t1 .. $t2 where actual anniversay has to be 
in to trigger anniversary display, basically:
         // $t1 = now - before days, adjusted to start of day
         // $t2 = now - after days, adjusted to end of day
         $now = new DateTime();
         $t1 = $now->sub($this->before);
         $t1->setTime(0, 0, 0);
         $now = new DateTime();
         $t2 = $now->add($this->after);
         $t2->setTime(23, 59, 59);
         // loop through all anniversary items
         foreach($this->annlist as $annitem) {
             // calculate actual anniversary time
             // $tsdt = start date + interval from anniversary item
             $tsdt = new DateTime();
             $tsdt->setTimestamp($ts);
             $anntim = $tsdt->add($annitem->interval);
             // test if $tsdt is in range $t1 .. $t2 and if so return 
anniversary description and date
             if($t1->getTimestamp() <= $anntim->getTimestamp() && 
$anntim->getTimestamp() <= $t2->getTimestamp()) {
                 return $annitem->descrip . ' ' . 
$anntim->format($this->format);
             }
         }
         // no anniversaries found so return null
         return null;
     }
}

// demo code:

function test($annpol, $ts) {
     $ann = $annpol->getAnniversary($ts);
     if($ann != null) {
         echo "$ann\r\n";
     } else {
         echo "No anniversary\r\n";
     }
}

$annpol = new AnniversaryPolicy(2, 2);
$annpol->add('1 year anniversay', 'P1Y');
$annpol->add('3 months anniversary', 'P3M');
$annpol->add('1 month anniversary', 'P1M');

test($annpol, time() - 28*24*60*60);
test($annpol, time() - 31*24*60*60);
test($annpol, time() - 34*24*60*60);
test($annpol, time() - 82*24*60*60);
test($annpol, time() - 92*24*60*60);
test($annpol, time() - 102*24*60*60);
test($annpol, time() - 255*24*60*60);
test($annpol, time() - 365*24*60*60);
test($annpol, time() - 375*24*60*60);

?>

Back to dk.edb.database | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-13 13:54 +0000
  Re: PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-13 14:44 +0000
  Re: PHP/MySQL problem med timestamp Arne Vajhøj <arne@vajhoej.dk> - 2018-01-13 14:42 -0500
    Re: PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-15 16:41 +0000
      Re: PHP/MySQL problem med timestamp Arne Vajhøj <arne@vajhoej.dk> - 2018-01-15 11:52 -0500
        Re: PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-16 21:27 +0000
          Re: PHP/MySQL problem med timestamp Arne Vajhøj <arne@vajhoej.dk> - 2018-01-16 19:56 -0500
  Re: PHP/MySQL problem med timestamp Jan Hansen <jhjjhjhhansen@gmail.com> - 2018-01-17 03:26 +0000
    Re: PHP/MySQL problem med timestamp Jan Hansen <jhjjhjhhansen@gmail.com> - 2018-01-17 04:32 +0000
    Re: PHP/MySQL problem med timestamp (rettet udgave) Jan Hansen <jhjjhjhhansen@gmail.com> - 2018-01-17 04:42 +0000
    Re: PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-17 10:15 +0000
      Re: PHP/MySQL problem med timestamp Jan Hansen <jhjjhjhhansen@gmail.com> - 2018-01-17 12:28 +0000
        Re: PHP/MySQL problem med timestamp "Dennis Munding" <nospa@mplease.dk> - 2018-01-20 14:27 +0000

csiph-web