-1

I want to have the desired output as such 20170613 which is an integer. I know using strtotime() I can get an UNIX timestamp as an integer, but I don't want that. date("Ymd") however returns a string.

I can't seem to figure a way to convert this to an integer.

Edit #1: Here is what I am attempting: $x = (int)date("Ymd"); echo $x;

The result however does not show up in the browser. Infact in the developer's tools, it shows internal server error.

Akriti Anand
  • 166
  • 3
  • 17
  • just cast it... – enno.void Jun 14 '17 at 15:20
  • You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Jun 14 '17 at 15:20
  • `Type casting`.... there's a section about it in the [PHP Documentation](http://www.php.net/manual/en/language.types.type-juggling.php) – Mark Baker Jun 14 '17 at 15:22

1 Answers1

1

The term to Google is "type cast". That leads you to the PHP type juggling docs on integer casting.

Taking that as a reference point, the canonical way to go about it is:

$int = (int)date('Ymd');

For completeness, you could also use the equivalent full form:

$int = (integer)date('Ymd');

Or the functional:

$int = intval(date('Ymd'));
bishop
  • 37,830
  • 11
  • 104
  • 139
  • and in the year 9223372036854775807 (PHP_INT_MAX) , his code will _SILENTLY_ break. to make sure the year 9223372036854775807 maintainer is promptly warned, and making sure the error does not propagate, you should do ```$int = filter_var(date('Ymd'),FILTER_VALIDATE_INT);if(false===$int){throw new \LogicException('failed to convert date to php integer!');}``` – hanshenrik Jun 14 '17 at 15:25