# Convert date-object to a proper string

**URL:** <https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 6, 2015, 2:39pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413 "2015-11-06T14:39:09Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 2:39pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/1 "2015-11-06T14:39:09Z")

</div>

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](http://getkirby.com/docs/cheatsheet/field-methods/int)**;

`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…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 2:45pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/2 "2015-11-06T14:45:03Z")

</div>

```php
$page->date('n')

```

should give you the number without the leading zero.

[http://php.net/manual/de/function.date.php](http://php.net/manual/de/function.date.php)

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [November 6, 2015, 3:02pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/3 "2015-11-06T15:02:40Z")

</div>

`j` would be the day, you probably meant `n`.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 3:06pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/4 "2015-11-06T15:06:03Z")

</div>

> [@lukasbestle](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 3:16pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/5 "2015-11-06T15:16:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 3:23pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/6 "2015-11-06T15:23:07Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![PaulMorel](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paulmorel/32/371_2.png) [@PaulMorel](https://forum.getkirby.com/u/PaulMorel)\
**Post date:** [November 6, 2015, 5:05pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/7 "2015-11-06T17:05:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 6, 2015, 5:11pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/8 "2015-11-06T17:11:51Z")

</div>

`->int()` does the following:

```php
$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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 5:32pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/9 "2015-11-06T17:32:11Z")

</div>

```php
$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.

---

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 5:42pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/10 "2015-11-06T17:42:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 6, 2015, 5:43pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/11 "2015-11-06T17:43:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 5:44pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/12 "2015-11-06T17:44:16Z")

</div>

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

Problem solved… again!

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 6, 2015, 5:48pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/13 "2015-11-06T17:48:26Z")

</div>

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

```php
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 😆

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 5:49pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/14 "2015-11-06T17:49:11Z")

</div>

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

```auto
$page->date()

```

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

While

```auto
$page->date('n');

```

etc.  
returns a string.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 6, 2015, 5:58pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/15 "2015-11-06T17:58:40Z")

</div>

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

```php
  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](https://github.com/getkirby/kirby/pull/314)

---

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 6:16pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/16 "2015-11-06T18:16:38Z")

</div>

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

```php
/* 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" */

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 6, 2015, 6:22pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/17 "2015-11-06T18:22:49Z")

</div>

Why don’t you use

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

```

---

<div class="post-metadata">

**Author:** ![anon68757951](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anon68757951](https://forum.getkirby.com/u/anon68757951)\
**Post date:** [November 6, 2015, 6:37pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/18 "2015-11-06T18:37:57Z")

</div>

…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](http://countdownjs.org/demo.html)**… I was just looking for an universal solution to convert **any** value (date-string or not) to an integer…

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:04pm UTC](https://forum.getkirby.com/t/convert-date-object-to-a-proper-string/2413/19 "2025-02-06T17:04:17Z")

</div>


