Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Time and Date solely next to DateTime (2.0.0-dev) #148

Open
melroy89 opened this issue Mar 8, 2019 · 2 comments
Open

Support Time and Date solely next to DateTime (2.0.0-dev) #148

melroy89 opened this issue Mar 8, 2019 · 2 comments

Comments

@melroy89
Copy link

melroy89 commented Mar 8, 2019

Currently with the 2.0.0-dev branch, the birthday, anniversary you need to specify a new \DateTime as constructor parameter.

The problem with this, is you can't specify a date only (without time). Resulting in a vcard output (file) with a full date + time (yyyymmddThhmmssZ), which is not always wanted.

Eg. for a birthday the date is enough, which should result in yyyymmdd only.

This discussion was started in #146. But I like to solve this within another PR.

Solution Branch: 2.0.0-dev

@melroy89 melroy89 changed the title Support Time and Date solely instead of DateTime (2.0.0-dev) Support Time and Date solely next to DateTime (2.0.0-dev) Mar 8, 2019
@helveticadomes
Copy link

could we use use

//--- instead ---//
new DateTime('2015-12-05');
//--- something like this ---//
date("Ymd", strtotime('2015-12-05'));

or will this create other issues?

@melroy89
Copy link
Author

melroy89 commented Mar 21, 2019

@helveticadomes Well, yes this will solve the fact you solely have a date. But now you can't support date + time (which should also be possible in some cases) 😢 .

if ($this->value instanceof \DateTime) {

As you can see here. A check is used to validate if the object is of time DateTime. If true, it will return a valid DATE-AND-OR-TIME rfc6350 format.

What you actually want is keep supporting this behavior (date + time behavior I mean), and on top of that support Time (without date). And support Date (without time).

This can be done in my eyes, by inheriting the DateTime class (thus use DateTime as base class) for example. An create your own 'Date' class and 'Time' class. In that case we could re-use DateTimeOrStringValue class/DateTimeOrStringValue.php.

class Date extends \DateTime {
 //empty
}

class Time extends \DateTime {
 //empty
}

And extend __toString() behavior with (example):

        if ($this->value instanceof \DateTime) {
            // According to DATE-AND-OR-TIME rfc6350 standard
            return $this->value->format('Ymd\THis');
        elseif ($this->value instanceof Time) {
            return $this->value->format('\THis');
        }
        elseif ($this->value instanceof Date) {
             return $this->value->format('Ymd');
        } else {
            return $this->value;
        }  

I hope I make sense?

Another alternative would be to extend it DateTime fully, and vcard project should not use DateTime directly anymore (which I also really like in modularity kind of view). So we have DateAndTime, Date and Time classes:

class DateAndTime extends \DateTime {
  public function __toString() {
    return $this->format('Ymd\THis');
  }
}

class Date extends \DateTime {
  public function __toString() {
    return $this->format('Ymd');
  }
}

class Time extends \DateTime {
   public function __toString() {
    return $this->format('\THis');
  }
}

In this last example DateTimeOrStringValue class properly is much easier in that case/should not contain any logic anymore. Since you just inherent the to_string already. Maybe only strval? I'm not sure..

My guess would be the whole __to_string method can be removed from this file, once you implement those three classes above:

public function __toString(): string

In fact, if we implement those three classes, we can properly get rid of this whole DateTimeOrStringValue.php file totally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants