Convert date-object to a proper string

I have a date object, populated with a date-field.

Something like this;

2015/07/24 (YY/MM/DD).

Now I need to write down the month as an integer - so I can manipulate the number using Javascript.

I tried the int-method;

echo $page->date('m')->int();

…but this throws a fatal error;

FATAL ERROR: CALL TO A MEMBER FUNCTION INT() ON STRING

How can I properly convert a string to an integer?

I solved it right now, doing so;

echo 1*$page->date('m')->int();

That works (it “converts” date 06 to a proper 6 without a leading zero), but I prever the official way…

$page->date('n')

should give you the number without the leading zero.

http://php.net/manual/de/function.date.php

j would be the day, you probably meant n.

You are so right, I was in the wrong line and it had the wrong description. Corrected it above.

Okay, that works - but does it return a string or an int?

I can not do maths on strings, only on integers… I hope my question is clear enough.

It returns a string, but you can convert to int:

(int)$page->date('n')

Is there a difference between type casting the variable vs using int() method?

->int() does the following:

$val = $field->empty() ? $default : $field->value;
return intval($val);

So you can pass also pass a default value (->int(9)) to use if the field is empty.

$page->date('n')->int();

throws an error (PHP Fatal error: Call to a member function int() on string).

Guess, it only works with a field value of type object, not of type string.

Exactly - that is the problem I encountered (see opening post).

->int(); works fine with a common text field, but used on a date field it throws an error.

Anyhow both (int)$page->date('n'); and 1*$page->date('n'); works fine.

Yea, that’s the problem with date since it isn’t (or at least not only) a field, but more a method as well, which returns a string and not a field object.

Cool, I prefer (int)$page->date('n'); because it comes closest to $page->date('n')->int();

Problem solved… again!

Wondering if we could change this line https://github.com/getkirby/kirby/blob/master/core/page.php#L728 to:

return new Field($this, $field, $timestamp);

Could anyone try if with this change $page->date('n')->int() would work? EDIT: Did myself, doesn’t work :laughing:

There’s also a difference in type depending on what you output:

$page->date()

returns an integer, i.e. the UNIX timestamp,

While

$page->date('n');

etc.
returns a string.

Okay, so the date function should be like this to work with ->int() (and other methods):

  public function date($format = null, $field = 'date') {

    if($timestamp = strtotime($this->content()->$field())) {

      if(!is_null($format)) {
        $timestamp = $this->kirby->options['date.handler']($format, $timestamp);
      }

      return new Field($this, $field, $timestamp);
    }

  }

Edit: opened a pull request https://github.com/getkirby/kirby/pull/314

Yeah, this is why I needed to convert the month (of a date field) to a proper string;

/* convert the published date of a news-article from
an integer (08), to it's textual equivalent (august) */

$publishMonth = array(null,'Januar',
    'Februar',
    'März',
    'April',
    'Mai',
    'Juni',
    'Juli',
    'August',
    'September',
    'Oktober',
    'November',
    'Dezember');

echo $publishMonth[$subpage->date('m')->int()];

/* should return an integer, so "08" would be written as "August" */

Why don’t you use

strftime($page->date('F'))

…because I needed the month notation language-independent (triggered / fired by a Javascript) and because I also needed to convert another string to an integer (not a date-field, but a textual field - containing an integer).

So I could use that integer as the value for a Javascript countdown-function… I was just looking for an universal solution to convert any value (date-string or not) to an integer…