0

This is my code.

$arrivDate = $_POST['arrival_date'];
echo $arrivDate; //Print correctly

echo date("Y/m/d", strtotime($arrivDate)) ; // every time print 1969/12/31
echo date("Y/m/d", strtotime('.$arrivDate.')) ; // every time print 1969/12/31

but if i put like this, it is print correctly

echo date("Y/m/d",  strtotime("05/08/2019")); // Print like this --> 2019/08/05 (This is correct)

My question is if i put hard coded value, it is print very well. but i cant put a variable to it. evry time print a garbage value. im sure that $_POST['arrival_date'] part has no any problem because according to my 2nd line, the correct value is shown.
Please tell me the reason for this....

3
Contributors
6
Replies
33
Views
4 Years
Discussion Span
Last Post by diafol
0

You don't show the format of the $_POST['arrival_date'].

You're better to use the DateTime object - create from a specified format and output to a new format. Easy.

$date = DateTime::createFromFormat('d/m/Y', $_POST['arrival_date']);
echo $date->format('Y/m/d');

Edited by diafol

0

My format is 'd/m/Y'.
the code is not working....
showing this error
Call to undefined method DateTime::createFromFormat() in //path
I think im using a lower version of PHP. Please tell me some other method to do this....

0

You need the php 5.3 verion for the DateTime::createFromFormat, it was introduced in that version.

Edited by Szabi Zsoldos

0

My format is 'd/m/Y'.

From the manual -

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

You're out of luck with strtotime.

You can easily replace the '/' with '-' though with str_replace.

Edited by diafol

0

This is what i did. Searching by searching i found this. This method can change the format of a date. My problem is solved. Thank you very much everyone for replying. I post my code because of some other begginer also can get idea from this....

        $arrivDate = $_POST['arrival_date'];
        list($day, $month, $year) = explode('/', $arrivDate);
        $timestamp = mktime(0, 0, 0, $month, $day, $year);
        $arrivDate =  date("Y-m-d", $timestamp);
1

You could have done this, as I suggested:

$arrivDate = str_replace("/","-",$_POST['arrival_date']);
$arrivDate = date("Y-m-d", strtotime($arrivDate));

Seems less hassle than creating an array and then a timestamp.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.