Reputation: 1211
I am trying to convert a string into a date. Specifically, a string of a year into a date.
strtotime('2017') // Gives us -> 1517372220
$year = '2017';
var_dump($year); // '2017'
$year = date("Y", strtotime($year));
var_dump($year); // '2018'
Why is the date method defaulting to the current time? I am passing an integer.
According to the documentation, I seem to implementing this method correctly?
string date ( string $format [, int $timestamp = time() ] )
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Upvotes: 1
Views: 97
Reputation: 72269
Based on strtotime() Manual
strtotime — Parse about any English textual datetime description into a Unix timestamp
Parameters
time-> A date/time string. Valid formats are explained in Date and Time Formats.
So either you need to provide a textual representation or a valid date format (not only years).
So code need to be like this:-
<?php
$year = date("Y", strtotime('2017-01-01'));
var_dump($year); // '2018'
Output:- https://eval.in/945561
Reference:- valid formats for strtotime()
Upvotes: 1